Skip to content

Can't style a table. #32

Closed
Closed

Description

@hyunlee1o
    table.style = document.styles["Table Grid"]
                  ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/home/user/folder/.direnv/python-3.11.8/lib/python3.11/site-packages/docx/styles/styles.py", line 57, in __getitem__
    raise KeyError("no style with name '%s'" % key)
KeyError: "no style with name 'Table Grid'"

I have done some research and the object CT_Styles that is used in my Document i think something is wrong with it.
Being self an instance of CT_Styles

(Pdb) pp [style.name.val for style in self._iter_styles()]
['Normal',
 'heading 1',
 'heading 2',
 'heading 3',
 'heading 4',
 'heading 5',
 'Default Paragraph Font',
 'Normal Table',
 'No List',
 'Title',
 'Title Char',
 'Heading 1 Char',
 'Heading 2 Char',
 'Heading 3 Char',
 'Heading 4 Char',
 'Heading 5 Char',
 'Subtitle',
 'Subtitle Char',
 'Subtle Emphasis',
 'Emphasis',
 'Intense Emphasis',
 'Strong',
 'Quote',
 'Quote Char',
 'Intense Quote',
 'Intense Quote Char',
 'Subtle Reference',
 'Intense Reference',
 'Book Title',
 'List Paragraph',
 'No Spacing',
 'List Number',
 'List Bullet',
 'macro',
 'Macro Text Char',
 'Mention',
 'Message Header',
 'Message Header Char',
 'Plain Text',
 'Plain Text Char']

I tested the same in python-docx and this happened:

(Pdb) styles = [item.name.val for item in self._iter_styles()]
(Pdb) styles
['Normal', 'header', 'Header Char', 'footer', 'Footer Char', 'heading 1', 'heading 2', 'heading 3', 'heading 4', 'heading 5', 'heading 6', 'heading 7', 'heading 8', 'heading 9', 'Default Paragraph Font', 'Normal Table', 'No List', 'No Spacing', 'Heading 1 Char', 'Heading 2 Char', 'Heading 3 Char', 'Title', 'Title Char', 'Subtitle', 'Subtitle Char', 'List Paragraph', 'Body Text', 'Body Text Char', 'Body Text 2', 'Body Text 2 Char', 'Body Text 3', 'Body Text 3 Char', 'List', 'List 2', 'List 3', 'List Bullet', 'List Bullet 2', 'List Bullet 3', 'List Number', 'List Number 2', 'List Number 3', 'List Continue', 'List Continue 2', 'List Continue 3', 'macro', 'Macro Text Char', 'Quote', 'Quote Char', 'Heading 4 Char', 'Heading 5 Char', 'Heading 6 Char', 'Heading 7 Char', 'Heading 8 Char', 'Heading 9 Char', 'caption', 'Strong', 'Emphasis', 'Intense Quote', 'Intense Quote Char', 'Subtle Emphasis', 'Intense Emphasis', 'Subtle Reference', 'Intense Reference', 'Book Title', 'TOC Heading', 'Table Grid', 'Light Shading', 'Light Shading Accent 1', 'Light Shading Accent 2', 'Light Shading Accent 3', 'Light Shading Accent 4', 'Light Shading Accent 5', 'Light Shading Accent 6', 'Light List', 'Light List Accent 1', 'Light List Accent 2', 'Light List Accent 3', 'Light List Accent 4', 'Light List Accent 5', 'Light List Accent 6', 'Light Grid', 'Light Grid Accent 1', 'Light Grid Accent 2', 'Light Grid Accent 3', 'Light Grid Accent 4', 'Light Grid Accent 5', 'Light Grid Accent 6', 'Medium Shading 1', 'Medium Shading 1 Accent 1', 'Medium Shading 1 Accent 2', 'Medium Shading 1 Accent 3', 'Medium Shading 1 Accent 4', 'Medium Shading 1 Accent 5', 'Medium Shading 1 Accent 6', 'Medium Shading 2', 'Medium Shading 2 Accent 1', 'Medium Shading 2 Accent 2', 'Medium Shading 2 Accent 3', 'Medium Shading 2 Accent 4', 'Medium Shading 2 Accent 5', 'Medium Shading 2 Accent 6', 'Medium List 1', 'Medium List 1 Accent 1', 'Medium List 1 Accent 2', 'Medium List 1 Accent 3', 'Medium List 1 Accent 4', 'Medium List 1 Accent 5', 'Medium List 1 Accent 6', 'Medium List 2', 'Medium List 2 Accent 1', 'Medium List 2 Accent 2', 'Medium List 2 Accent 3', 'Medium List 2 Accent 4', 'Medium List 2 Accent 5', 'Medium List 2 Accent 6', 'Medium Grid 1', 'Medium Grid 1 Accent 1', 'Medium Grid 1 Accent 2', 'Medium Grid 1 Accent 3', 'Medium Grid 1 Accent 4', 'Medium Grid 1 Accent 5', 'Medium Grid 1 Accent 6', 'Medium Grid 2', 'Medium Grid 2 Accent 1', 'Medium Grid 2 Accent 2', 'Medium Grid 2 Accent 3', 'Medium Grid 2 Accent 4', 'Medium Grid 2 Accent 5', 'Medium Grid 2 Accent 6', 'Medium Grid 3', 'Medium Grid 3 Accent 1', 'Medium Grid 3 Accent 2', 'Medium Grid 3 Accent 3', 'Medium Grid 3 Accent 4', 'Medium Grid 3 Accent 5', 'Medium Grid 3 Accent 6', 'Dark List', 'Dark List Accent 1', 'Dark List Accent 2', 'Dark List Accent 3', 'Dark List Accent 4', 'Dark List Accent 5', 'Dark List Accent 6', 'Colorful Shading', 'Colorful Shading Accent 1', 'Colorful Shading Accent 2', 'Colorful Shading Accent 3', 'Colorful Shading Accent 4', 'Colorful Shading Accent 5', 'Colorful Shading Accent 6', 'Colorful List', 'Colorful List Accent 1', 'Colorful List Accent 2', 'Colorful List Accent 3', 'Colorful List Accent 4', 'Colorful List Accent 5', 'Colorful List Accent 6', 'Colorful Grid', 'Colorful Grid Accent 1', 'Colorful Grid Accent 2', 'Colorful Grid Accent 3', 'Colorful Grid Accent 4', 'Colorful Grid Accent 5', 'Colorful Grid Accent 6']
(Pdb) "Table Grid" in styles
True

I think you are missing something.

Activity

  1. toxicphreAK commented on Aug 3, 2026

    @toxicphreAK
    Owner

    Fixed by the 2.0.0 restart from upstream v1.2.0 — no separate change was needed.

    The cause was the bundled default.docx: the 0.9.x template shipped a reduced style set of ~39 styles that did not include Table Grid. The template on main carries 164, and the line from this report now works:

    >>> d = docx.Document()
    >>> t = d.add_table(rows=2, cols=2)
    >>> t.style = d.styles["Table Grid"]
    >>> t.style.name
    'Table Grid'

    It also survives a save/reopen round trip. Verified on main at 2.0.0.dev0.

    Note for anyone arriving here from a related symptom: a handful of built-ins — header, footer, caption, heading 6–heading 9 — are still absent from the bundled template, because Word only materialises a style definition once the style is used. That is what #23 is about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions