如何使用 python 和 python-docx 在 header/footer 中添加 table

How to add a table inside a header/footer with python and python-docx

我想在我的 header 中添加一行三列的 table。有什么建议吗?

我试试:

from docx import Document

document = Document()
section = document.sections[0]
header = section.header
table = header.add_table(rows=1, cols=3)  # method not yet implemented !

document.save('test.docx')

但是显然不行

header.add_table() 已实施并且应该可以工作。该实施是最近的(几个月前),因此请尝试升级您的 python-docx 安装:

pip install -U python-docx

注意 .add_table() 需要三个参数,一个行数,一个列数和一个宽度,所以像这样:

from docx.shared import Inches

table = header.add_table(1, 3, Inches(6))

该方法的文档在此处:
https://python-docx.readthedocs.io/en/latest/api/section.html#header-and-footer-objects