-1

I can't seem to find a way to change the length f the entire table in a word-document. I have only seen examples of ways to change the columns in the table, not the actual table itself.

Would be great if someone could tell me how to do it :)

Here is my code:

from docx import Document
document = Document()
table = document.add_table(rows=4, cols=2)
table.style = 'Table Grid'
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pigfarmer
  • 1
  • 3

2 Answers2

0

The Table class has methods to add rows.

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

Ilayaraja
  • 2,388
  • 1
  • 9
  • 9
  • Thanks for the answer! I don't see how this might help me, I want to make my table smaller, not add another row. PLS explain how I should use that to make "less wide" – Pigfarmer Jan 23 '18 at 14:18
  • Check whether this helps: https://python-docx.readthedocs.io/en/latest/api/table.html#docx.table.Table.autofit – Ilayaraja Jan 23 '18 at 14:24
  • No, dose not help :( I need something like "table.width(Inches(3))" – Pigfarmer Jan 24 '18 at 07:50
0

Found a solution to my problem. I got this from ANOTHER user here @ stack. Can't seem to find the link tho....

The original code is NOT mine, I only modified it a little.

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