Generate Automated Resignation Letters with Python‑Docx: Step‑by‑Step Guide
This article walks you through installing python‑docx, creating a Word document, adding headings, paragraphs, images, tables, and custom text formatting, and finally automating the generation of a resignation letter using a reusable template in Python.
Introduction
Hello, I’m Wu (吴老板) and I’m sharing a Python automation tutorial.
Python is a great tool for automation, for example automatically generating resignation letters.
Installation
pip install python-docx -i https://pypi.douban.com/simple/Basic Usage
Create a docx document
from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml.ns import qn
from docx.shared import RGBColor, Pt
document = Document()
paragraph = document.add_heading('开空调', level=3)
document.save('开空调.docx')Add a paragraph
# LEFT => left align
# CENTER => center
# RIGHT => right align
# JUSTIFY => justify
paragraph = document.add_paragraph("前几天最热的时候我们家热得快炸了!!!")
paragraph_format = paragraph.paragraph_format
paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
paragraph.add_run('粗体').bold = TrueAdd an image
from docx.shared import Inches, RGBColor
paragraph = document.add_paragraph()
paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
run = paragraph.add_run("")
run.add_picture('./ico.jpg', width=Inches(1.0), height=Inches(1.0))Set font color and size
paragraph = document.add_paragraph()
paragraph.add_run('那你开')
paragraph.add_run('空调').font.color.rgb = RGBColor(54, 95, 145)
paragraph.add_run('啊!!')
paragraph.add_run('开空调!!').font.size = Pt(30) # font sizeInsert a table
document.add_page_break()
table = document.add_table(rows=2, cols=2)
table.style = 'LightShading-Accent1'
cell = table.cell(0, 1)
cell.text = '这是第一行第二列的单元格'
row = table.rows[1]
row.cells[0].text = '第二行第一列'
row.cells[1].text = '第二行第二列'Generate a resignation report
# -*- coding: utf-8 -*-
"""
@Time :2021/2/1 10:51
@Auth :wutong
@File :自动离职报告.py
@IDE :PyCharm
"""
from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml.ns import qn
from docx.shared import RGBColor, Pt, Inches
from lzStr import lzStr
# Initialize document
document = Document()
# Set global font
document.styles['Normal'].font.name = u'.萍方-简'
document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'.萍方-简')
# Add title
paragraph = document.add_heading('离职申请', level=3)
paragraph_format = paragraph.paragraph_format
paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
# Add image
paragraph = document.add_paragraph()
paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
run = paragraph.add_run("")
run.add_picture('./image.jpg', width=Inches(1.0), height=Inches(1.0))
# Add personalized text
paragraph = document.add_paragraph()
lz_add_run = paragraph.add_run(lzStr.format(name="吴老板"))
lz_add_run.font.size = Pt(8)
lz_add_run.font.color.rgb = RGBColor(54, 95, 145)
# Save file
document.save('离职.docx')Template file (lzStr.py)
# -*- coding: utf-8 -*-
"""
@Time :2021/2/1 10:53
@Auth :wutong
@File :lz.py
@IDE :PyCharm
"""
"""此模板来自网络,做了小小改动 !!!"""
lzStr = ''' 你好啊! 我({name})已决定于2050年4月15日正式离职,
所以特提前半月向您提交这份辞职申请。
还请留意一下简历中是否有适合接替我工作的人选,
感激你这几年对我的照顾和帮忙,十分感激!
最近在整理自我的简历,突然发觉这几年我在公司做的工种实在太多了,
细数一下,有不下十种。
先是1900年3月从前台开始做起,
然后是人事,行政,市场开拓,婚庆,护肤品牌信息收集,再到之后的外贸。
还有兼职文案,校对,送货,以及各种各样的手工。
并且大多数时候我同时在做好几件事情,
虽然我从来没有抱怨过我的工作太多可是这并不表示我的工作量小。
并且我所有的工作都有做完的那天,或者是被人接手的那一天,
这也就意味着一个新的工作即将来临。
有时候就觉得自我是个高级打杂工,真的太杂了,
杂到我此刻已经搞不清楚我自我能干什么,想干什么,
我此刻对自我的职业定位和前程也是一片迷茫。
所以,我此刻想休息一下,为自我的将来好好打算一下,
重新规划自我的职业和人生。
记得年前已经跟你谈过一次,谈过我的想法,
所以想必我的辞职对你来说应当不会太意外。
我是公司刚起步不久就过来帮你创业了,
到此刻为止已经做了快3年多了,
看着公司一天天成长壮大,慢慢的走上正轨我心里也是蛮高兴的。
老实说离开公司多少还是有些不舍。我明白你的想法点子也很多,
以前每一次你有新想法我都很支持你,
都会全心全意按着你的想法去做事。
公司出现了什么问题,我都会尽我最大的努力去帮你处理好
因为我很重承诺,我答应的事情必须会做到。
此刻公司已经走上正轨并且新鲜血液不断供给,
我想我能够激流勇退了,也请你给我一次机会,
一次让我实现自我想法的机会。
{name} 2050-04-15 04:15:15
'''Running the script creates a file named 离职.docx in the current directory; an example of the generated resignation letter is shown below.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
