使用 Python 在 word docx 中编辑 table
Edit table in word docx using Python
如何使用 Python 编辑 word 文档中已经存在的 table。
假设在我的 word 文档中我有一个只有 2 行的 table,我想在 Python 中添加更多行,我该怎么做?我试过 docx
库,但我能做的最好的就是创建一个 table 并将其保存到 word 文档。
我想编辑一个已经存在的 table。谢谢!
代码
您可以在 docx 库中执行此操作,方法是从 Document
.
访问 .tables
from docx import Document
doc = Document('grid.docx')
doc.tables #a list of all tables in document
print("Retrieved value: " + doc.tables[0].cell(0, 0).text)
doc.tables[0].cell(0, 0).text = "new value"
doc.tables[0].add_row() #ADD ROW HERE
doc.save("grid2.docx")
来自
至
如何使用 Python 编辑 word 文档中已经存在的 table。
假设在我的 word 文档中我有一个只有 2 行的 table,我想在 Python 中添加更多行,我该怎么做?我试过 docx
库,但我能做的最好的就是创建一个 table 并将其保存到 word 文档。
我想编辑一个已经存在的 table。谢谢!
代码
您可以在 docx 库中执行此操作,方法是从 Document
.
.tables
from docx import Document
doc = Document('grid.docx')
doc.tables #a list of all tables in document
print("Retrieved value: " + doc.tables[0].cell(0, 0).text)
doc.tables[0].cell(0, 0).text = "new value"
doc.tables[0].add_row() #ADD ROW HERE
doc.save("grid2.docx")