在 *.docx 文件的 header 中创建一个 table
Create a table in the header of a *.docx file
我有以下工作代码 usinf python-docx
:
import docx
inp = input('inp: ')
def write_docx_header():
document = docx.Document('Test.docx')
section = document.sections[0]
header = section.header
paragraph = header.paragraphs[0]
paragraph.text = inp + "\t" + inp + "\t" + inp
paragraph.style = document.styles["Header"]
document.save('Test.docx')
write_docx_header()
现在我想要一个 table 在 header 中有三列而不是三个选项卡,但我不知道从哪里开始以及如何做。
非常感谢
records = (
(3, 'Spam', 'Meow'),
(7, 'Eggs', 'Meow'),
(4, 'Spam, spam, eggs, and spam', 'Meow')
)
table = document.add_table(rows=1, cols=3)
table.style = 'TableGrid' # apply styling
header_cells = table.rows[0].cells
header_cells[0].text = 'ID'
header_cells[1].text = 'Name'
header_cells[2].text = 'Cat'
for id, desc, meow in records:
row_cells = table.add_row().cells
row_cells[0].text = str(id)
row_cells[1].text = desc
row_cells[2].text = meow
document.save('Test.docx')
我有以下工作代码 usinf python-docx
:
import docx
inp = input('inp: ')
def write_docx_header():
document = docx.Document('Test.docx')
section = document.sections[0]
header = section.header
paragraph = header.paragraphs[0]
paragraph.text = inp + "\t" + inp + "\t" + inp
paragraph.style = document.styles["Header"]
document.save('Test.docx')
write_docx_header()
现在我想要一个 table 在 header 中有三列而不是三个选项卡,但我不知道从哪里开始以及如何做。
非常感谢
records = (
(3, 'Spam', 'Meow'),
(7, 'Eggs', 'Meow'),
(4, 'Spam, spam, eggs, and spam', 'Meow')
)
table = document.add_table(rows=1, cols=3)
table.style = 'TableGrid' # apply styling
header_cells = table.rows[0].cells
header_cells[0].text = 'ID'
header_cells[1].text = 'Name'
header_cells[2].text = 'Cat'
for id, desc, meow in records:
row_cells = table.add_row().cells
row_cells[0].text = str(id)
row_cells[1].text = desc
row_cells[2].text = meow
document.save('Test.docx')