import os
from docx import Document
from docx.shared import Pt,RGBColor
from docx.oxml.ns import qn
#旧docx文件目录
old_file_path = 'E:\pro\doc'
#生成新文件目录
new_file_path = 'E:\pro\docx'
replace_dict ={
'第一范文网':'汇文网',
'第一范文':'汇文网'
} #替换字典
def check_and_change(document,replace_dict):
for para in document.paragraphs:
# lastep = document.paragraphs[len(document.paragraphs) - 1] #清除段落,最后一段
# lastep.clear()
for i in range(len(para.runs)): #替换关键词
for key,value in replace_dict.items():
if key in para.runs[i].text:
print(key+"-->"+value)
para.runs[i].text = para.runs[i].text.replace(key,value)
else:
pass
for run in para.runs: #设置文档格式
run.font.bold = False # 黑体
run.font.italic = False # 斜体
run.font.underline = False # 下划线
run.font.strike = False # 删除线
run.font.shadow = False # 阴影
run.font.size = Pt(16) # 字体大小
# run.font.color.rgb = RGBColor(0, 0, 0) # 字体颜色
if run.font.color.rgb == RGBColor(255,0,0): #删除红色字体
run.clear()
run.font.name = '仿宋_GB2312' # 字体
r = run._element.rPr.rFonts # 中文字体
r.set(qn('w:eastAsia'), '仿宋_GB2312')
# paragraph.alignment = WD_ALIGN_PARAGRAPH.LEFT # 对齐方式
para.paragraph_format.line_spacing = Pt(28) #用浮点数表示两倍行距
para.paragraph_format.space_before = Pt(12) #表示12磅
para.paragraph_format.space_after = Pt(28) #表示28磅
# print(para.style.name,para.text) #获取标题文字
style_name = para.style.name
if style_name.startswith('Heading'): #修改标题属性
for a in para.runs:
a.font.size = Pt(20)
a.font.bold = True
a.font.name = '宋体'
if len(para.text) == 0: #删除空白行,固定写法
p = para._element
p.getparent().remove(p)
p._p = p._element = None
return document
def main():
for name in os.listdir(old_file_path):
print(name)
old_file = old_file_path + '/'+name
new_file = new_file_path + '/'+name
if old_file.split('.')[-1] == 'docx': #触发替换函数,必须是docx格式
document = Document(old_file)
document = check_and_change(document,replace_dict)
document.save(new_file)
print('done')
if __name__ == '__main__':
main()