应用 python-docx 库写一个 Word 文档的辞职函,对于 python-docx 的根底利用参考后面两节内容,次要包含题目、疏导语、注释、署名局部辞职函内容,明天的内容有点干欢送发问、交换。
1# -*- coding: UTF-8 -*-
2
3from docx import Document
4
5
6def run():
7 # 创立 word 对象
8 doc_ = Document()
9 # 字体款式
10 from docx.oxml.ns import qn
11 doc_.styles['Normal'].font.name = u'宋体'
12 # 设置题目
13 paragraph_ = doc_.add_paragraph()
14 run_ = paragraph_.add_run(u"辞职函")
15 # 获取题目的字体对象
16 font_ = run_.font
17 # 加粗
18 font_.bold = True
19
20 from docx.shared import Pt
21 # 设置字体大小 30 英镑
22 font_.size = Pt(20)
23 # 设置程度居中
24 from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
25 paragraph_format_title = paragraph_.paragraph_format
26 paragraph_format_title.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
27 # 增加问候
28 doc_.add_paragraph(u"尊敬的领导:")
29 # 增加注释
30 content_ = u"此时辞呈,敬请海涵,当初公司募才纳新,未嫌我满腹经纶,承蒙收留,入职以来,领导厚爱,共事相亲,"
31 u"至今心怀感怀,不敢稍有忘记。勤心劳作,不求闻达贫贱,薪酬所愿,只需饥寒残喘,欲效犬马,以图恩报。时至今日,"
32 u"大失所望,本当保持,然孝亲养身,责任在肩,今日请辞,实属无奈。提携之恩,栽培之情,铭刻五内。"
33 paragraph = doc_.add_paragraph(content_)
34 paragraph_format_content = paragraph.paragraph_format
35
36 # 设置首行缩进
37 from docx.shared import Inches
38 paragraph_format_content.first_line_indent = Inches(0.25)
39
40 # 增加署名
41 paragraph_format_name_format = doc_.add_paragraph(u"公众号:【Python 集中营】").paragraph_format
42 paragraph_format_name_format.left_indent = Inches(4)
43
44 # 保存文档
45 doc_.save(u'C:/Python 集中营 -- 辞职函.docx')
46
47
48if __name__ == '__main__':
49 run()
更多精彩返回微信公众号【Python 集中营】,专一后端编程实战,原创文章每天更新!