【问题标题】:How to change font size of all .docx document with python-docx如何使用 python-docx 更改所有 .docx 文档的字体大小
【发布时间】:2019-08-18 22:00:21
【问题描述】:

所以,我试图用 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')

【问题讨论】:

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


    【解决方案1】:

    您需要迭代运行。

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

    【讨论】:

    • 有效!非常感谢!我不能赞成你的回答很有用,因为我的声望不到 15...
    • 欢迎您,您可以接受答案。无需赞成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2015-03-07
    • 2015-07-01
    相关资源
    最近更新 更多