如何使用python-docx更改所有.docx文档的字体大小

时间:2019-08-17 14:02:51

标签: python xml python-3.x ms-word python-docx

因此,我正在尝试解决Python Docx的问题。我需要重构我的.docx文档,我需要更改所有文档的字体名称和字体大小。您能提出什么解决方案?

使用此代码,“字体名称”正在更改,但“字体大小”没有更改。

from docx import Document
from docx.shared import Pt
document = Document('path/to/file.docx')


style = document.styles['Normal']
font = style.font
font.name = 'Arial'
font.size = Pt(10)
for paragraph in document.paragraphs:
    paragraph.style = document.styles['Normal']
document.save('refactored.docx')

1 个答案:

答案 0 :(得分:0)

您需要遍历运行。

for paragraph in document.paragraphs:
paragraph.style = document.styles['Normal']
for run in paragraph.runs:
    run.font.size = Pt(10)