我有一个简单的脚本来使用 python docx 生成文档。我可以很好地生成文档,但是当我尝试更改与文档部分相关的任何内容时,我收到错误:
错误 - 处理时出错:“Twips”对象不可调用
from docx import Document
from docx.shared import Inches
from docx.section import Section
doc = Document()
section = doc.sections[0]
section.page_width(Inches(5))
section.page_height(Inches(5))
我尝试了不同的单位和调用部分数组的方法。我假设创建的文档默认已经有一个部分,但我也尝试在它之前调用 doc.add_section() ,并且发生了同样的问题。
似乎这不是正确的用法:
section.page_width(Inches(5))
section.page_height(Inches(5))
请尝试:
section.page_width = Inches(5)
section.page_height = Inches(5)