I am trying to prepend a full page image, no margins, to an existing docx, using python-docx.
It is my understanding that the code should go something like this (using the solution suggested 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')
There are two problems with this:
- As-is, the script changes the left margin for the whole document. With the last two lines uncommented, the margin for the front page changes back to 1in.
- The new section created a the beginning of the script creates a blank page at the beginning of the document.
How do I do it correctly? Thx.