用python-docx修改docx页边距。

问题描述 投票:8回答:2

我需要快速改变许多docx文档的页边距。我检查了python-docx,但没有找到修改页面布局(尤其是页边距)属性的方法。有什么方法吗?

python python-docx
2个回答
25
投票

感谢 @tdelaney 指出的那一页,在那里它清楚地表明了解决方案。

#Open the document
document = Document(args.inputFile)

#changing the page margins
sections = document.sections
for section in sections:
    section.top_margin = Cm(margin)
    section.bottom_margin = Cm(margin)
    section.left_margin = Cm(margin)
    section.right_margin = Cm(margin)

document.save(args.outputFile)

0
投票
import docx
from docx.shared import Inches, Cm
doc = docx.Document()
sections = doc.sections
for section in sections:
    section.top_margin = Cm(0.5)
    section.bottom_margin = Cm(0.5)
    section.left_margin = Cm(1)
    section.right_margin = Cm(1)

以下是我使用的代码,请包含以下内容 从docx.shared导入Inches, Cm。

© www.soinside.com 2019 - 2024. All rights reserved.