Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Logo in Header of Word document using python-docx

I want a logo file to be attached everytime in the word document, when I run the code,

Ideally the code should look like :

from docx import Document
document = Document()
logo = open('logo.eps', 'r')                  #the logo path that is to be attached
document.add_heading('Underground Heating Oil Tank Search Report', 0) #simple heading that will come bellow the logo in the header.
document.save('report for xyz.docx')              #saving the file

is this possible in the python-docx or should i try some other library to do this? if possible please tell me how,

like image 537
Tayyab Nasir Avatar asked Sep 19 '25 15:09

Tayyab Nasir


1 Answers

A simpler way to include logo and a header with some style (Heading 2 Char here):

from docx import Document
from docx.shared import Inches, Pt

doc = Document()

header = doc.sections[0].header
paragraph = header.paragraphs[0]

logo_run = paragraph.add_run()
logo_run.add_picture("logo.png", width=Inches(1))

text_run = paragraph.add_run()
text_run.text = '\t' + "My Awesome Header" # For center align of text
text_run.style = "Heading 2 Char"
like image 97
bhatiaravi Avatar answered Sep 23 '25 09:09

bhatiaravi