江东的笔记

Be overcome difficulties is victory

0%

Python修改论文的字体及其大小

使用python读取数据并修改文章字体相关格式和大小

对标题的格式修改

·首先是导入包和读取word文档

1
2
3
4
5
6
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 not in title:
for kuai in run.runs:
if kuai.text not in 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'),'黑体')

最后不要忘记保存文件

1
file.save("E:/aa.docx")

(在设计全文数据库系统项目中,将论文进行最后的标准化)