# 适合比较长的模板,并且保留原格式
from docx import Document
from docx.shared import Pt
from docx.oxml.ns import qn
document = Document(r'C:/Users/13375/Desktop/python/长恨歌2.docx')
document.styles['Normal'].font.name = u'微软雅黑'
document.styles['Normal'].element.rPr.rFonts.set(qn('w:eastAsia'),u'微软雅黑')
document.styles['Normal'].font.size = Pt(22)
def change_text(old_text,new_text):
all_paragraphs = document.paragraphs
for paragraph in all_paragraphs:
for run in paragraph.runs:
run_text = run.text.replace(old_text,new_text)
run.text = run_text
all_tables = document.tables
for table in all_tables:
for row in table.rows:
for cell in row.cells:
cell_text = cell.text.replace(old_text,new_text)
cell.text = cell_text
change_text('不','OK')
change_text('女','male')
document.save(r'C:/Users/13375/Desktop/python/长恨歌修改后.docx')
相关文章
- 02-05WORD中字数和字符
- 02-05网络流--最大流ek模板
- 02-05cajviewer逆向分析-HN文件格式分析和010editor模板开发
- 02-05高斯消元模板
- 02-05快排模板
- 02-05860. 染色法判定二分图(模板)
- 02-05leetcode面试准备: Word Pattern
- 02-05线性同余方程(同余+扩展欧几里得模板)
- 02-05dede模板标签帮助[DedeCMS教程]
- 02-05算法模板——Tarjan强连通分量