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

How to change font size of all .docx document with 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')

您需要迭代运行。

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