更改 table word 文档的长度,python-docx,Python

Change table length in word-document, python-docx, Python

我似乎找不到改变 word 文档中整个 table 长度的方法。我只看到了更改 table 中列的方法示例,而不是实际的 table 本身。

如果有人能告诉我怎么做就太好了:)

这是我的代码:

from docx import Document
document = Document()
table = document.add_table(rows=4, cols=2)
table.style = 'Table Grid'

Table class 有添加行的方法。

https://python-docx.readthedocs.io/en/latest/api/table.html#docx.table.Table.add_row

找到了解决我的问题的方法。我从@ stack 的另一个用户那里得到了这个。似乎找不到 link 不过....

原始代码不是我的,我只是稍微修改了一下。

def ChangeWidthOfTable(table,width,column):
  for columnVarible in range(0,column):
      for cell in table.columns[columnVarible].cells:
          cell.width = Cm(width)