# Python 自动化修改word的案例 **Published by:** [个人记录](https://paragraph.com/@polkdefi/) **Published on:** 2022-05-08 **URL:** https://paragraph.com/@polkdefi/python-word ## Content from docx import Document from docx.shared import Cm from docx.enum.text import WD_ALIGN_PARAGRAPH import re document=Document(r"g:\CS\Python Scripts\表扬信.docx")首先对段落格式进行修改,docx默认标题也属于段落,因此“表扬信”是第一段paragraphs=document.paragraphs paragraphs[2].paragraph_format.first_line_indent=Cm(0.74) paragraphs[3].paragraph_format.left_indent=Cm(0.74) paragraphs[4].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.RIGHT paragraphs[4].paragraph_format.right_indent=Cm(2) paragraphs[5].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.RIGHT paragraphs[5].paragraph_format.right_indent=Cm(2)对文本进行修改修改第二段paragraphs[1].text="小Z同学:"将第三段陆亦可替换为大Z,她替换为他。通过python的正则表达式,可以很简单地实现文本的替换和查找。text=re.sub('陆亦可','大Z',paragraphs[2].text) text=re.sub('她','他',text) paragraphs[2].text=text在第四段后面加上paragraphs[3].add_run("向小z同学学习!")修改表格里面的内容tables=document.tables tables[0].cell(1,0).text="猫粮" tables[0].cell(2,0).text="猫粮" tables[0].cell(3,0).text="猫粮"插入一张图片,图片宽度设置为11.8cmdocument.add_picture('fun.jpg', width=Cm(11.8)) document.save() ## Publication Information - [个人记录](https://paragraph.com/@polkdefi/): Publication homepage - [All Posts](https://paragraph.com/@polkdefi/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@polkdefi): Subscribe to updates - [Twitter](https://twitter.com/Alp2A): Follow on Twitter