简体   繁体   English

如何使用python-docx在docx文件中写入多个表?

[英]How to write multiple tables in docx file using python-docx?

I want to write/include more than two tables in a docx file using python.我想使用 python 在 docx 文件中写入/包含两个以上的表。 How can I write this structure as a table in a docx file using python?如何使用 python 在 docx 文件中将此结构编写为表格?

I tried the following code for a single table.我为单个表尝试了以下代码。 Now I would like to create 2 tables in a single docx.现在我想在一个 docx 中创建 2 个表。

table = document.add_table(rows=rows_no,cols=1)

Timeline = row[5]
print (row[0],row[3],"Timing:",row[5])
cells = table.add_row().cells
cells[0].paragraphs[0].add_run( Compliance_requirements).bold = True
cells[0].paragraphs[1].add_run( "Obs: "+Finding_Description).text = True
cells[0].paragraphs[2].add_run( "requitements: "+requirements).text = True
cells[0].paragraphs[3].add_run( "Timeline: Need"+Timeline+" days of notice period .").text = True

document.add_paragraph()

You just need a loop to create your tables.你只需要一个循环来创建你的表。 Let's say you need 5 tables:假设您需要 5 个表:

number_of_tables = [1, 2, 3, 4, 5]
document = Document()

for k in number_of_tables:
    table = document.add_table(rows=2, cols=3, style='Light Grid')
    # Here add your content

    document.add_paragraph('Adding space between tables')

document.save('my_test.docx')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM