Python 3.x 使用PythonDocx在Word文档的标题中添加徽标

Python 3.x 使用PythonDocx在Word文档的标题中添加徽标,python-3.x,python-docx,Python 3.x,Python Docx,我希望每次在word文档中运行代码时都附加一个徽标文件 理想情况下,代码应该如下所示: 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) #sim

我希望每次在word文档中运行代码时都附加一个徽标文件

理想情况下,代码应该如下所示:

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

在PythonDocx中,这是可能的,还是应该尝试其他库来实现这一点?如果可能,请告诉我如何使用以下代码创建一个包含两列的表。第一个元素是徽标,第二个元素是标题的文本部分

from docx import Document
from docx.shared import Inches, Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
document = 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.RIGHT
document.save('yourdoc.docx')

包含徽标和带有某种样式的标题(此处标题2字符)的更简单方法:

Word中的“页眉”是每页的上边距区域中的一块文本,通常在每页上显示相同的内容。“标题”通常是一个“章节标题”,是一个标题段落,通常比正文粗体且大,用于介绍新章节。你想要哪个?
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"