我正在尝试使用python-docx在现有的docx之前添加整页图像,不添加任何边距。
据我了解,代码应该像这样(使用建议的解决方案previously)
from docx import Document
from docx.shared import Inches
document = Document('existing.docx')
new_doc = Document()
new_section = new_doc.add_section()
new_section.left_margin = Inches(0.3)
new_doc.add_picture('frontpage.jpg', width=Inches(8.0))
for element in document.element.body:
new_doc.element.body.append(element)
# for section in new_doc.sections[1:]:
# section.left_margin = Inches(1.0)
new_doc.save('new.docx')
这有两个问题:
如何正确执行?谢谢。
调用.add_section()
在文档末尾添加一个新节,由分页符分隔。
使用现有部分来设置第一部分的属性,然后添加第二部分并根据文档其余部分的需要调整其属性。
document.sections[0]
上提供了新的默认文档中的现有单节。