5

I used this code:

# open a document
doc = docx.Document()

# add a table to the end and create a reference variable
# extra row is so we can add the header row
t = doc.add_table(df.shape[0]+1, df.shape[1])
t.allow_autofit = True
t.style = 'TableGrid'
t.alignment=WD_TABLE_ALIGNMENT.CENTER

Where df is some pandas DataFrame. But the columns are not auto-fitting.

enter image description here

user1581390
  • 1,900
  • 5
  • 25
  • 38
  • "Either algorithm will adjust column widths proportionately when total table width exceeds page width." Could it be the case that you are exceeding the page's width? – Zev Jun 25 '18 at 02:37
  • I don't think so, this is a table with values of 3 digits... – user1581390 Jun 25 '18 at 14:27
  • @Zev: I've added the screenshot. This is Word 2013 do you happen to have a different version? – user1581390 Jun 26 '18 at 20:01
  • I was thinking that you were saying the columns were too small rather than too large so I misunderstood. I get what you are looking for now and am trying a fix. – Zev Jun 26 '18 at 20:14

0 Answers0