26.Python操作Word和Powerpoint文件
Python操作Word和PowerPoint文件
在日常工作中,有很多简单重复的劳动其实完全可以交给 Python 程序,比如根据样板文件(模板文件)批量的生成很多个 Word 文件或 PowerPoint 文件。Word 是微软公司开发的文字处理程序,相信大家都不陌生,日常办公中很多正式的文档都是用 Word 进行撰写和编辑的,目前使用的 Word 文件后缀名一般为.docx。PowerPoint 是微软公司开发的演示文稿程序,是微软的 Office 系列软件中的一员,被商业人士、教师、学生等群体广泛使用,通常也将其称之为“幻灯片”。在 Python 中,可以使用名为python-docx 的三方库来操作 Word,可以使用名为python-pptx的三方库来生成 PowerPoint。
操作Word文档
我们可以先通过下面的命令来安装python-docx三方库。
按照官方文档的介绍,我们可以使用如下所示的代码来生成一个简单的 Word 文档。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| from docx import Document from docx.shared import Cm, Pt
from docx.document import Document as Doc
document = Document()
document.add_heading('快快乐乐学Python', 0)
p = document.add_paragraph('Python是一门非常流行的编程语言,它') run = p.add_run('简单') run.bold = True run.font.size = Pt(18) p.add_run('而且') run = p.add_run('优雅') run.font.size = Pt(18) run.underline = True p.add_run('。')
document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='Intense Quote')
document.add_paragraph( 'first item in unordered list', style='List Bullet' ) document.add_paragraph( 'second item in ordered list', style='List Bullet' )
document.add_paragraph( 'first item in ordered list', style='List Number' ) document.add_paragraph( 'second item in ordered list', style='List Number' )
document.add_picture('resources/guido.jpg', width=Cm(5.2))
document.add_section()
records = ( ('骆昊', '男', '1995-5-5'), ('孙美丽', '女', '1992-2-2') )
table = document.add_table(rows=1, cols=3) table.style = 'Dark List' hdr_cells = table.rows[0].cells hdr_cells[0].text = '姓名' hdr_cells[1].text = '性别' hdr_cells[2].text = '出生日期'
for name, sex, birthday in records: row_cells = table.add_row().cells row_cells[0].text = name row_cells[1].text = sex row_cells[2].text = birthday
document.add_page_break()
document.save('demo.docx')
|
提示:上面代码第7行中的注释# type: Doc是为了在PyCharm中获得代码补全提示,因为如果不清楚对象具体的数据类型,PyCharm 无法在后续代码中给出Doc对象的代码补全提示。
执行上面的代码,打开生成的 Word 文档,效果如下图所示。

对于一个已经存在的 Word 文件,我们可以通过下面的代码去遍历它所有的段落并获取对应的内容。
1 2 3 4 5 6
| from docx import Document from docx.document import Document as Doc
doc = Document('resources/离职证明.docx') for no, p in enumerate(doc.paragraphs): print(no, p.text)
|
提示:如果需要上面代码中的 Word 文件,可以通过下面的百度云盘地址进行获取。链接:https://pan.baidu.com/s/1rQujl5RQn9R7PadB2Z5g_g 提取码:e7b4。
读取到的内容如下所示。
1 2 3 4 5 6 7 8 9 10
| 0 1 离 职 证 明 2 3 兹证明 王大锤 ,身份证号码: 100200199512120001 ,于 2018 年 8 月 7 日至 2020 年 6 月 28 日在我单位 开发部 部门担任 Java开发工程师 职务,在职期间无不良表现。因 个人 原因,于 2020 年 6 月 28 日起终止解除劳动合同。现已结清财务相关费用,办理完解除劳动关系相关手续,双方不存在任何劳动争议。 4 5 特此证明! 6 7 8 公司名称(盖章):成都风车车科技有限公司 9 2020 年 6 月 28 日
|
讲到这里,相信很多读者已经想到了,我们可以把上面的离职证明制作成一个模板文件,把姓名、身份证号、入职和离职日期等信息用占位符代替,这样通过对占位符的替换,就可以根据实际需要写入对应的信息,这样就可以批量的生成 Word 文档。
按照上面的思路,我们首先编辑一个离职证明的模板文件,如下图所示。
接下来我们读取该文件,将占位符替换为真实信息,就可以生成一个新的 Word 文档,如下所示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| from docx import Document from docx.document import Document as Doc
employees = [ { 'name': '骆昊', 'id': '100200198011280001', 'sdate': '2008年3月1日', 'edate': '2012年2月29日', 'department': '产品研发', 'position': '架构师', 'company': '成都华为技术有限公司' }, { 'name': '王大锤', 'id': '510210199012125566', 'sdate': '2019年1月1日', 'edate': '2021年4月30日', 'department': '产品研发', 'position': 'Python开发工程师', 'company': '成都谷道科技有限公司' }, { 'name': '李元芳', 'id': '2102101995103221599', 'sdate': '2020年5月10日', 'edate': '2021年3月5日', 'department': '产品研发', 'position': 'Java开发工程师', 'company': '同城企业管理集团有限公司' }, ]
for emp_dict in employees: doc = Document('resources/离职证明模板.docx') for p in doc.paragraphs: if '{' not in p.text: continue for run in p.runs: if '{' not in run.text: continue start, end = run.text.find('{'), run.text.find('}') key, place_holder = run.text[start + 1:end], run.text[start:end + 1] run.text = run.text.replace(place_holder, emp_dict[key]) doc.save(f'{emp_dict["name"]}离职证明.docx')
|
执行上面的代码,会在当前路径下生成三个 Word 文档,如下图所示。
生成PowerPoint
首先我们需要安装名为python-pptx的三方库,命令如下所示。
用 Python 操作 PowerPoint 的内容,因为实际应用场景不算很多,我不打算在这里进行赘述,有兴趣的读者可以自行阅读python-pptx的官方文档,下面仅展示一段来自于官方文档的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| from pptx import Presentation
pres = Presentation()
title_slide_layout = pres.slide_layouts[0] slide = pres.slides.add_slide(title_slide_layout)
title = slide.shapes.title subtitle = slide.placeholders[1]
title.text = "Welcome to Python" subtitle.text = "Life is short, I use Python"
bullet_slide_layout = pres.slide_layouts[1] slide = pres.slides.add_slide(bullet_slide_layout)
shapes = slide.shapes
title_shape = shapes.title body_shape = shapes.placeholders[1]
title_shape.text = 'Introduction'
tf = body_shape.text_frame tf.text = 'History of Python'
p = tf.add_paragraph() p.text = 'X\'max 1989' p.level = 1
p = tf.add_paragraph() p.text = 'Guido began to write interpreter for Python.' p.level = 2
pres.save('test.pptx')
|
运行上面的代码,生成的 PowerPoint 文件如下图所示。
总结
用 Python 程序解决办公自动化的问题真的非常酷,它可以将我们从繁琐乏味的劳动中解放出来。写这类代码就是去做一件一劳永逸的事情,写代码的过程即便不怎么愉快,使用这些代码的时候应该是非常开心的。