PaddlePaddle/PaddleOCR · error · ValueError

Unable to apply style {self.paragraph_style}.

Error message

Unable to apply style {self.paragraph_style}.

What it means

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.

Source

Thrown at ppstructure/recovery/table_process.py:237

            raise ValueError("Second argument needs to be a %s" % docx.table._Cell)
        unwanted_paragraph = cell.paragraphs[0]
        if unwanted_paragraph.text == "":
            delete_paragraph(unwanted_paragraph)
        self.set_initial_attrs(cell)
        self.run_process(html)
        # cells must end with a paragraph or will get message about corrupt file
        # https://stackoverflow.com/a/29287121
        if not self.doc.paragraphs:
            self.doc.add_paragraph("")

    def apply_paragraph_style(self, style=None):
        try:
            if style:
                self.paragraph.style = style
            elif self.paragraph_style:
                self.paragraph.style = self.paragraph_style
        except KeyError as e:
            raise ValueError(f"Unable to apply style {self.paragraph_style}.") from e

    def handle_table(self, html, doc):
        """
        To handle nested tables, we will parse tables manually as follows:
        Get table soup
        Create docx table
        Iterate over soup and fill docx table with new instances of this parser
        Tell HTMLParser to ignore any tags until the corresponding closing table tag
        """
        table_soup = BeautifulSoup(html, "html.parser")
        rows, cols_len = get_table_dimensions(table_soup)
        table = doc.add_table(len(rows), cols_len)
        table.style = doc.styles["Table Grid"]

        num_rows = len(table.rows)
        num_cols = len(table.columns)

        cell_row = 0

View on GitHub (pinned to 2661c7c0ef)

Solutions

  1. Use a style name that exists in the document: open the docx and check [s.name for s in doc.styles]
  2. Create the style first: from docx.enum.style import WD_STYLE_TYPE; doc.styles.add_style('MyStyle', WD_STYLE_TYPE.PARAGRAPH)
  3. Pass style=None / omit paragraph_style so no assignment is attempted
  4. Fix the message bug: raise ValueError(f"Unable to apply style {self.paragraph_style}.") (drop stray quotes)

Example fix

# before
parser = TableToDocx(doc, paragraph_style='My Custom Style')  # KeyError -> ValueError

# after
if 'My Custom Style' not in [s.name for s in doc.styles]:
    doc.styles.add_style('My Custom Style', WD_STYLE_TYPE.PARAGRAPH)
parser = TableToDocx(doc, paragraph_style='My Custom Style')
Defensive patterns

Strategy: validation

Validate before calling

existing = {s.name for s in doc.styles}
if paragraph_style and paragraph_style not in existing:
    doc.styles.add_style(paragraph_style, WD_STYLE_TYPE.PARAGRAPH)
parser = TableToDocx(doc, paragraph_style=paragraph_style)

Type guard

def style_exists(doc, name: str) -> bool:
    return any(s.name == name for s in doc.styles)

Try / catch

try:
    parser.apply_paragraph_style(style)
except ValueError as e:
    if 'Unable to apply style' in str(e):
        parser.paragraph_style = None  # degrade to default style
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14). Data as JSON: /api/errors/6b32d4b67a26b115. Report an issue: GitHub.