from docx import Document import re from docx.shared import Pt, RGBColor # 字号,颜色 from docx.oxml.ns import qn # 中文字体
file = Document("E:\\File\\大一\\大一下学期/马克思.docx")
然后对字体进行修改
1 2 3 4 5 6
for run in file.paragraphs: if re.match('^Heading \d+$', run.style.name): # 找出所有标题 for kuai in run.runs: kuai._element.rPr.rFonts.set(qn('w:eastAsia'), '黑体') kuai.font.size = Pt(42) # 修改字号 kuai.font.bold = True# 加粗
对文章内容对修改
·对正文的修改:
1 2 3 4 5 6
for run in file.paragraphs: if run.style.name == "Normal": for kuai in run.runs: kuai._element.rPr.rFonts.set(qn('w:eastAsia'), '黑体') kuai.font.size = Pt(42) print(run.text)
由于是对整篇论文进行修改,所以又改变了一下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
title = [] # 存入非段落内容数据 for run in file.paragraphs: if re.match('^Heading \d+$', run.style.name): title.append(run.text) elif run.style.name == "Normal": title.append(run.text) for run in file.paragraphs: if run.text notin title: for kuai in run.runs: if kuai.text notin title: print(kuai) # kuai.font.size = Pt(42) kuai.font.color.rgb = RGBColor(200, 100 , 200) # kuai.font.name = 'Arial' # kuai._element.rPr.rFonts.set(qn('w:eastAsia'),'黑体')