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

des*_*oid 1 python xml ms-word python-3.x 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')
Run Code Online (Sandbox Code Playgroud)

Moh*_*dam 8

您需要迭代运行以更改字体。

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