import docx |
from docx.shared import Pt #用于设定字体大小(磅值) |
from docx.oxml.ns import qn #用于应用中文字体 |
def Info_update(doc,old_info, new_info): |
for para in doc.paragraphs: |
para.text = para.text.replace(old_info, new_info) |
|
#设置第一段(标题)的文字格式 |
for run in doc.paragraphs[ 0 ].runs: |
run.font.size = Pt( 14 ) #文字大小磅值 |
run.bold = True #加粗 |
run.font.name = "微软雅黑" #字体选择 |
#中文字体应用,固定写法 |
r = run._element.rPr.rFonts #字体,固定写法 |
r. set (qn( "w:eastAsia" ), "微软雅黑" ) #字体 |
|
#设置第二及后续段落的文字格式 |
for para in doc.paragraphs[ 1 :]: |
for run in para.runs: |
run.font.size = Pt( 12 ) #文字大小 |
run.bold = False #不加粗 |
run.font.name = "微软雅黑" #字体选择 |
#中文字体应用,固定写法 |
r = run._element.rPr.rFonts |
r. set (qn( "w:eastAsia" ), "微软雅黑" ) |
|
doc = docx.Document( '替换前.docx' ) |
Info_update(doc, "第四次" , "第五次" ) |
Info_update(doc, "2019" , "2020" ) |
Info_update(doc, "18" , "10" ) |
doc.save( '替换后_设置格式.docx' ) |