简体   繁体   English

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

[英]How to change font size of all .docx document with python-docx

So, Im trying to solve problem with Python Docx.所以,我试图用 Python Docx 解决问题。 I need to refactor my .docx document, I need to change Font Name and Font Size of all document.我需要重构我的 .docx 文档,我需要更改所有文档的字体名称和字体大小。 What solutions can you suggest?您能提出什么解决方案?

With this code Font Name are changing, but Font Size are not.使用此代码字体名称正在更改,但字体大小没有。

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')

You need to iterate over runs for font changes.您需要迭代运行以更改字体。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM