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 = op
Solution 1:
with the following code, you can create a table with two columns the first element is the logo, the second element is the text part of the header
from docx importDocumentfrom docx.sharedimportInches, Ptfrom docx.enum.textimportWD_ALIGN_PARAGRAPHdocument = Document()
header = document.sections[0].header
htable=header.add_table(1, 2, Inches(6))
htab_cells=htable.rows[0].cells
ht0=htab_cells[0].add_paragraph()
kh=ht0.add_run()
kh.add_picture('logo.png', width=Inches(1))
ht1=htab_cells[1].add_paragraph('put your header text here')
ht1.alignment = WD_ALIGN_PARAGRAPH.RIGHTdocument.save('yourdoc.docx')
Solution 2:
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"
Post a Comment for "Adding Logo In Header Of Word Document Using Python-docx"