Python-docx table column width

379 Views Asked by At

I am trying to generate a table for reporting, and I want the Table in a generated word document. The problem is, that the width of the table columns I set in the python script, only adjusts the width of the columns which are wider than one Inch, every column which I want to be 0,5 Inches is set to the default width of 1 Inch. I´ve tried to set the width of the columns in Cm, Inches and EMUs, but It´s always the same problem, I think one possibility is, that I am using a table.style 'Colorful List' and that the style resets some of the columns, but not all of them. here´s the code-snipped, with the column width in Cm. If you have any Ideas on how to set the column width with using a table.style please help, If you need more Information about what I want to do, do not hesitate to ask. Kind regards Carl `

<kbd># import the necessary modules

from openpyxl import workbook, load_workbook
import modules
import docx
import datetime
import pandas as pd
from openpyxl.utils import get_column_letter
from docxtpl import DocxTemplate
from docx import Document
from docx.shared import RGBColor
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT, WD_TABLE_ALIGNMENT
from docx.dml.color import ColorFormat
from docx.enum.dml import MSO_COLOR_TYPE
from docx.enum.text import WD_COLOR_INDEX
from docx.enum.text import WD_COLOR
from docx.shared import Pt
from docx.oxml.ns import nsdecls
from docx.oxml import parse_xml
import re
from docx.shared import Inches
import docx
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.table import WD_ALIGN_VERTICAL
from docx.enum.table import WD_ROW_HEIGHT_RULE
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
from docx.shared import Emu
from docx.shared import Cm

# create a new Word document

doc = Document()

doc.add_heading('Projektübersicht',0)

# add a table to the document

table = doc.add_table(rows=1, cols=6)

# Adding style to a table

table.style = 'Colorful List'

table.autofit = False
table.allow_autofit = False
for column in table.columns:
column.width = None

# add table header

header = table.rows\[0\].cells
header\[0\].text = 'Nr.'
header\[1\].text = 'Teilprojekt'
header\[2\].text = 'Kurzbeschreibung'
header\[3\].text = 'M'
header\[4\].text = 'M Name'
header\[5\].text = 'Status'

# set exact column widths

table.autofit = False
table.allow_autofit = False
table.width = Cm(15.8)  # total width of the table
table.columns\[0\].width = Cm(1.5)
table.columns\[1\].width = Cm(3)
table.columns\[2\].width = Cm(7)
table.columns\[3\].width = Cm(1.8)
table.columns\[4\].width = Cm(1.8)
table.columns\[5\].width = Cm(1.5)

# iterate through the projects and add each project as a new row in the table

for project in projects:
row = table.add_row().cells
row\[0\].text = str(project.number)
row\[1\].text = str(project.name)
row\[2\].text = str(project.kurzbeschreibung)
row\[3\].text = str(project.m1)
row\[4\].text = str(project.m1name)

I tried it with EMUs

table.autofit = False
table.allow_autofit = False

table.columns\[0\].width = 145685  # width of the first column in EMUs
table.columns\[1\].width = 425196  # width of the second column in EMUs
table.columns\[2\].width = 1915960  # width of the third column in EMUs
table.columns\[3\].width = 170591    # width of the fourth column in EMUs
table.columns\[4\].width = 226788    # width of the fifth column in EMUs
table.columns\[5\].width = 226788    # width of the sixth column in EMUs

and with Inches.

YES, When I used Inches, I imported Inches, and for Cm I imported Cm`</kbd>
1

There are 1 best solutions below

0
Dazack On

I think you need to set the individual width for a cell instead of the full column. I was unable to find any source that a full column could be set.

I was having a similar problem however resolved it by using the following:

def set_column_width(column, width):
    for cell in column.cells:
        cell.width = width

table.autofit = False
table.allow_autofit = False

set_column_width(table.columns[0], docx.shared.Cm(2.5))
set_column_width(table.columns[2], docx.shared.Cm(3.2))

Also why are you escaping your columns, this should not be needed

Resource https://github.com/python-openxml/python-docx/issues/78