【问题标题】:how write serial number of the docx table in python如何在python中写入docx表的序列号
【发布时间】:2021-04-08 18:30:31
【问题描述】:

我有很多输入,我想将它们写在带有序列号的 .docx 表中

def write():

    table = doc.add_table(rows=1, cols=3)
    table.style = "Table Grid"


    row = table.rows[0].cells
    row[0].text = "name and surname"
    row[1].text = "relative"
    row[2].text = "year of birth"


    for name_surname, relative, year_of_birth in data:
        row = table.add_row().cells
        row[0].text = name_surname
        row[1].text = relative
        row[2].text = year_of_birth

【问题讨论】:

  • 你的问题是什么?
  • 我无法保存带有序列号的 docx.table 文件
  • 您问题中的代码有效吗?它会创建一个表吗?应该很清楚如何修改它以添加具有行号的另一列。

标签: python list docx python-docx


【解决方案1】:

如果该代码有效,那么这应该可以满足您的需求。

def write():

    table = doc.add_table(rows=1, cols=4)
    table.style = "Table Grid"


    row = table.rows[0].cells
    row[0].text = "id"
    row[1].text = "name and surname"
    row[2].text = "relative"
    row[3].text = "year of birth"


    for idno, (name_surname, relative, year_of_birth) in enumerate(data):
        row = table.add_row().cells
        row[0].text = str(idno+1)
        row[1].text = name_surname
        row[2].text = relative
        row[3].text = year_of_birth

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多