【问题标题】:How to write multiple tables in docx file using python-docx?如何使用 python-docx 在 docx 文件中编写多个表?
【发布时间】:2021-04-29 00:03:55
【问题描述】:

我想使用 python 在 docx 文件中写入/包含两个以上的表。 如何使用 python 将此结构写为 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()

【问题讨论】:

    标签: python python-3.x python-docx


    【解决方案1】:

    您只需要一个循环来创建您的表格。假设您需要 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')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      相关资源
      最近更新 更多