Python Forum
How to create a table with different sizes of columns in MS word
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a table with different sizes of columns in MS word
#2
Hola Pepe!

This was tricky. Especially because, if you open this .docx file in Libre Office, the table has 3 equal-width columns! Libre Office seems to ignore the column size settings. For a while, I was going crazy!

But if you open it in MS Word the columns have the correct widths!

I just made a 1 row table to see if the column widths are set correctly.

import the imports, then just run myApp() Of course, you need to set your path for filename!

from docx import Document
from docx.shared import Pt, Mm, Cm, Inches
from docx.enum.table import WD_ALIGN_VERTICAL, WD_TABLE_ALIGNMENT

def myApp():
    filename = "/home/pedro/myPython/docxFiles/example_table.docx"
    doc = Document()
    heading = 'Shiny new but awkward table" \n\n'
    doc.add_heading(heading, 4)
    table = doc.add_table(rows=1, cols=3)
    table.alignment = WD_TABLE_ALIGNMENT.CENTER
    table.autofit = False
    table.allow_autofit = False
    table.style = 'Table Grid'
    headers = ["Name", "Age", "Occupation"]
    widths = [15.9, 11.8, 26.7]
    for col, col_data in enumerate(headers):
        print(col, col_data)
        table.cell(0,col).text = col_data
       
    for col, width in enumerate(widths):
       print(col, width)
       table.cell(0, col).width = Mm(widths[col])
    doc.save(filename)
I think you need these 2:

Quote:table.autofit = False
table.allow_autofit = False

I had to start Windows and open the file with MS Word, then it displays correctly.

Now you just need to set the column widths to the largest string length, as you were doing.

However, if a string is too long, your table will run off the page. So you better set a maximum column width, I think.
Reply


Messages In This Thread
RE: How to create a table with different sizes of columns in MS word - by Pedroski55 - Dec-02-2023, 11:36 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create Choices from .ods file columns cspower 3 683 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  Create csv file with 4 columns for process mining thomaskissas33 3 824 Nov-06-2023, 09:36 PM
Last Post: deanhystad
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 810 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Printing effect sizes for variables in an anova eyavuz21 2 1,034 Feb-01-2023, 02:12 PM
Last Post: eyavuz21
  Output difference from 2 lists of different sizes with words gracenz 5 1,395 Sep-02-2022, 05:09 PM
Last Post: Larz60+
  group by create pivot table python dawid294 1 1,343 Jun-22-2022, 06:13 PM
Last Post: Larz60+
  Sum the values in a pandas pivot table specific columns klllmmm 1 4,735 Nov-19-2021, 04:43 PM
Last Post: klllmmm
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,575 Aug-12-2021, 04:25 PM
Last Post: palladium
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,222 Jul-02-2021, 02:19 PM
Last Post: xtc14
  Create SQLite columns from a list or tuple? snakes 6 8,869 May-04-2021, 12:06 PM
Last Post: snakes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020