python-openxml / python-docx

Create and modify Word documents with Python
MIT License
4.63k stars 1.13k forks source link

Insert image in all pages in docx or header #407

Open gmmaraccini opened 7 years ago

gmmaraccini commented 7 years ago

The example below finds the first table of the DOCX File and inserts an image into it.

tables = doc.tables
p = tables[0].rows[0].cells[0].add_paragraph()
r = p.add_run()
r.add_picture(img,width=Inches(1), height=Inches(1))
doc.save('addImage3.docx')

I would like to add a table like this at the beginning of each page of the docx file and insert the image the same way.

I'm doing this because I did not find way to add header using python-docx

pysailor commented 5 years ago

@gmmaraccini I believe with the recent release (0.8.8) you will now be able to add / edit the header and footer. See https://python-docx.readthedocs.io/en/latest/user/hdrftr.html

You can insert an image either into a run of a paragraph in the header, or you could first insert a table into the header and then add an image into the table (very much like in your example).