Skip to content

TableStyle is overwritten by document style #1161

Open
@buhtz

Description

@buhtz

It seems to me that the "global" document style do overwrite "lower" styles e.g. TableStyle. I assume I misunderstand the concept here.

See this code as an example

#!/usr/bin/env python3
import os
import docx

document = docx.Document()
document.styles['Normal'].font.name = 'Fira Mono'
document.styles['Normal'].font.size = docx.shared.Pt(8)

my_tab_style = document.styles.add_style('MyTab', docx.enum.style.WD_STYLE_TYPE.TABLE)
my_tab_style.font.name = 'Fira Sans'
my_tab_style.font.size = docx.shared.Pt(20)

p = document.add_paragraph('Should be Fira Mono 8pt')

tab = document.add_table(rows=1, cols=1, style='MyTab')
tab.rows[0].cells[0].text = 'Should be Fira Sans 20pt'

document.save('test.docx')
os.system('start test.docx')

I would expect the document text in "Fira Mono 8pt" and the table content in "Fira Sans 20pt". But it isn't.
image

When you outcomment the document style lines

# document.styles['Normal'].font.name = 'Fira Mono'
# document.styles['Normal'].font.size = docx.shared.Pt(8)

the table style is taken into account.
image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions