{"record":{"id":"6b32d4b67a26b115","repo":"PaddlePaddle/PaddleOCR","slug":"unable-to-apply-style-self-paragraph-style","errorCode":null,"errorMessage":"Unable to apply style {self.paragraph_style}.","messagePattern":"Unable to apply style (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"ppstructure/recovery/table_process.py","lineNumber":237,"sourceCode":"            raise ValueError(\"Second argument needs to be a %s\" % docx.table._Cell)\n        unwanted_paragraph = cell.paragraphs[0]\n        if unwanted_paragraph.text == \"\":\n            delete_paragraph(unwanted_paragraph)\n        self.set_initial_attrs(cell)\n        self.run_process(html)\n        # cells must end with a paragraph or will get message about corrupt file\n        # https://stackoverflow.com/a/29287121\n        if not self.doc.paragraphs:\n            self.doc.add_paragraph(\"\")\n\n    def apply_paragraph_style(self, style=None):\n        try:\n            if style:\n                self.paragraph.style = style\n            elif self.paragraph_style:\n                self.paragraph.style = self.paragraph_style\n        except KeyError as e:\n            raise ValueError(f\"Unable to apply style {self.paragraph_style}.\") from e\n\n    def handle_table(self, html, doc):\n        \"\"\"\n        To handle nested tables, we will parse tables manually as follows:\n        Get table soup\n        Create docx table\n        Iterate over soup and fill docx table with new instances of this parser\n        Tell HTMLParser to ignore any tags until the corresponding closing table tag\n        \"\"\"\n        table_soup = BeautifulSoup(html, \"html.parser\")\n        rows, cols_len = get_table_dimensions(table_soup)\n        table = doc.add_table(len(rows), cols_len)\n        table.style = doc.styles[\"Table Grid\"]\n\n        num_rows = len(table.rows)\n        num_cols = len(table.columns)\n\n        cell_row = 0","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/ppstructure/recovery/table_process.py#L219-L255","documentation":"Raised by apply_paragraph_style in the docx table processor when python-docx raises KeyError while assigning self.paragraph.style. python-docx raises KeyError when the style name does not exist in the document's styles.xml part. The original KeyError is chained via `from e`, so the traceback shows both. The f-string has no placeholder, so the message always shows the literal '{self.paragraph_style}' — a minor bug in the message itself.","triggerScenarios":"Constructing TableToDocx with a table_style/paragraph_style name (e.g. 'Table Grid', 'Normal') that is not defined in the target .docx template, then processing HTML that triggers apply_paragraph_style.","commonSituations":"Using a custom or blank docx template that lacks built-in English style names; non-English Word/locales where built-in styles are named differently ('Tabela' vs 'Table Grid'); typos in the style argument; python-docx version differences in style lookup.","solutions":["Use a style name that exists in the document: open the docx and check [s.name for s in doc.styles]","Create the style first: from docx.enum.style import WD_STYLE_TYPE; doc.styles.add_style('MyStyle', WD_STYLE_TYPE.PARAGRAPH)","Pass style=None / omit paragraph_style so no assignment is attempted","Fix the message bug: raise ValueError(f\"Unable to apply style {self.paragraph_style}.\") (drop stray quotes)"],"exampleFix":"# before\nparser = TableToDocx(doc, paragraph_style='My Custom Style')  # KeyError -> ValueError\n\n# after\nif 'My Custom Style' not in [s.name for s in doc.styles]:\n    doc.styles.add_style('My Custom Style', WD_STYLE_TYPE.PARAGRAPH)\nparser = TableToDocx(doc, paragraph_style='My Custom Style')","handlingStrategy":"validation","validationCode":"existing = {s.name for s in doc.styles}\nif paragraph_style and paragraph_style not in existing:\n    doc.styles.add_style(paragraph_style, WD_STYLE_TYPE.PARAGRAPH)\nparser = TableToDocx(doc, paragraph_style=paragraph_style)","typeGuard":"def style_exists(doc, name: str) -> bool:\n    return any(s.name == name for s in doc.styles)","tryCatchPattern":"try:\n    parser.apply_paragraph_style(style)\nexcept ValueError as e:\n    if 'Unable to apply style' in str(e):\n        parser.paragraph_style = None  # degrade to default style\n    else:\n        raise","preventionTips":["Verify style names against doc.styles before constructing the parser","Prefer built-in English style names in templates regardless of Word UI locale","Treat missing styles as non-fatal: fall back to the document default style"],"tags":["docx","styles","keyerror-chaining","ppstructure"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}