Search code examples
pythonms-wordopenxmlpython-docx

Unsetting Header Row in python-docx or setting first column as title column


I am generating multiple tables with the python-docx but I have issue that some tables are rotated == having headers in the first column instead of the first row.

Is there any possibility how to uncheck following option with the python-docx or with accessing xml directly?

Microsoft Word settings


Solution

  • I did found workaround how to achieve what I needed. It is possible to directly access each paragraph in each cell and set it manually to bold during creation.

    This example makes first column bold and leaving rest of the table normal:

    table = document.add_table(rows=0, cols=len(data))
    table.style = 'Table Grid'
    row_cells = table.add_row().cells
    # Some fake data
    row_cells[0].text = data[0]
    row_cells[1].text = data[1]
    row_cells[2].text = data[2]
    # ...
    # This gets Heading Row 1 paragraph and sets it to bold
    row_cells[0].paragraphs[0].runs[0].font.bold = True