Header 和 0.8.8 之前版本的页脚创建

Header and footer creation in versions before 0.8.8

是否有在 Microsoft Word (docx) 文件中添加 headers 和页脚的解决方法?

这些未在 python-docx 0.8.8 之前的版本中实现。

更具体地说,我想补充:

  1. 页码到页脚
  2. 一些随机文本 headers

理想代码如下所示:

from docx import Document

document = Document()

# Add header and footer on all pages

document.save("demo.docx")

它不是最优雅的(它需要您在 VBA 和 Python 之间导航),但您可以使用 win32com 库来利用 MS Word 功能。这当然需要一台安装了 MS Office 的 Windows 机器。

import win32com.client as win32
msword = win32.gencache.EnsureDispatch('Word.Application')
doc = msword.Documents.Add
doc.Sections(1).Footers(1).Range.Text = r'Text to be included'
doc.Sections(1).Footers(1).PageNumbers.Add()

您可以使用的解决方法之一是使用在 Word 中创建的模板文档。创建一个空白文档,添加任何 header 文本和带页码的页脚,然后保存文档。然后使用:

from docx import Document
document = Document("template.docx")
# Do your editing
document.save("demo.docx")

...您应该能够在保留 header 和页脚的同时编辑其他所有内容。

最终我认为这个解决方案对于页码问题非常有效。如果您需要每个文档的唯一 header 文本,事情可能会变得有点棘手。如果是这种情况,您可能想尝试直接编辑 docx 文件的 XML。您可以在终端中使用它:

unzip template.docx

... 将 docx XML 文件吐出到目录中。您也可以使用 zipfile 在 python:

内完成
import zipfile

document = zipfile.ZipFile("template.docx")
for xml in document.filelist:
    if "header" in xml.filename:
        read = document.read(xml.filename)
        print(read.decode())

print 语句将打印整个 XML 文件,但您应该能够找到这个花絮:

<w:r><w:t>ThisIsMyHeader</w:t></w:r>

这将是您 header 中的文本。您所要做的就是编辑 XML 文件,将文件重新组合在一起,然后将文件类型更改回 docx。

这显然是一个超级 hacky 的解决方法,不幸的是我无法让它完全工作,但如果你绝对需要它,它至少是朝着正确方向迈出的好一步。

祝你好运!

模板方法有效,其主要优点是它是真正的 cross-platform 解决方案。但是,它要求样式已经在文档中应用过一次

让我们考虑 python-docx 中玩具示例的(简化)版本 documentation page.

第一步涉及创建模板文档:

from docx import Document

document = Document()

document.add_heading('Document Title', 0)

p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True

document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='IntenseQuote')

document.add_paragraph(
    'first item in unordered list', style='ListBullet'
)
document.add_paragraph(
    'first item in ordered list', style='ListNumber'
)

document.save('demo.docx')

(请注意,您也可以在第一步中手动应用样式,而无需使用 python-docx,即在 Word 中。)

接下来,您在 Microsoft Word 中打开此 demo.docx

  1. 添加所需的header
  2. 从菜单中插入页码
  3. 保存文档

完成上述操作后,只需删除 demo.docx 文档的主要内容(但不包括 header 和页脚的内容!)并再次保存文件。

在第二步中,您调用 demo.docx 使用 python-docx 进行所需的更改:

from docx import Document

document = Document('demo.docx')

document.add_heading('A New Title for my Document', 0)

p = document.add_paragraph('A new paragraph having some plain ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True

document.add_heading('New Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='IntenseQuote')

document.add_paragraph(
    'first new item in unordered list', style='ListBullet'
)
document.add_paragraph(
    'first new item in ordered list', style='ListNumber'
)

document.save('demo.docx')

您甚至可以添加更多内容,例如 table 具有现有 table 样式:

from docx import Document

document = Document('demo.docx')

document.add_page_break()

recordset = [ [1, "101", "Spam"], [2, "42", "Eggs"], [3, "631", "Spam, spam, eggs, and spam"]]

table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'

for item in recordset:
    row_cells = table.add_row().cells
    row_cells[0].text = str(item[0])
    row_cells[1].text = str(item[1])
    row_cells[2].text = item[2]

table.style = 'ColorfulShading'

document.save('demo.docx')

当然,可以避免一直重复第一步,通过copying the customized file然后在那里进行必要的更改(例如demo_copy.docx)而不影响模板:

import shutil
shutil.copyfile('demo.docx', 'demo_copy.docx')

最后值得一提的是,您还可以使用自定义样式!有关如何使用 python-docx 和 table 样式 see here.

执行此操作的示例

这样的事情怎么样(感谢 Eliot K)

from docx import Document
import win32com.client as win32
import os.path
import tempfile

tempdir = tempfile.gettempdir()
msword = win32.gencache.EnsureDispatch('Word.Application')
tempfile = os.path.join(tempdir, "temp.doc")

document = Document()

document.save(tempfile)

doc = msword.Documents.Open(tempfile)

doc.Sections(1).Footers(1).Range.Text = r'Text to be included'
doc.Sections(1).Footers(1).PageNumbers.Add()
doc.SaveAs(tempfile, FileFormat = 0)

document = Document(tempfile)

也许不是最优雅的方法,但应该可以满足您的需要。 也许将丑陋的 save/load 代码隔离在代码中某个尘土飞扬的角落的函数中 ;-)

同样,确实需要 windows 台安装了 Microsoft Office 的机器。

祝你好运!

我一直在用它工作

from docx import Document
document = Document()

header = document.sections[0].header
header.add_paragraph('Test Header') 

header = document.sections[0].footer
header.add_paragraph('Test Footers')

https://python-docx.readthedocs.io/en/latest/dev/analysis/features/header.html