PaddlePaddle/PaddleOCR · error · KeyError

{} not found

Error message

{} not found

What it means

StyleDict is a dict subclass with parent-chain lookup used by the tablepyxl (HTML-table-to-xlsx) renderer: CSS-like style keys not present locally are fetched from the parent StyleDict, mimicking CSS inheritance. KeyError is raised only when the key exists neither in the dict nor anywhere up the parent chain. It means tablepyxl was asked for a style attribute (font, border, alignment property) that was never set on the cell or its ancestors.

Source

Thrown at ppstructure/table/tablepyxl/style.py:143

    return known_styles[style_and_format_string]


class StyleDict(dict):
    """
    It's like a dictionary, but it looks for items in the parent dictionary
    """

    def __init__(self, *args, **kwargs):
        self.parent = kwargs.pop("parent", None)
        super(StyleDict, self).__init__(*args, **kwargs)

    def __getitem__(self, item):
        if item in self:
            return super(StyleDict, self).__getitem__(item)
        elif self.parent:
            return self.parent[item]
        else:
            raise KeyError("{} not found".format(item))

    def __hash__(self):
        return hash(tuple([(k, self.get(k)) for k in self._keys()]))

    # Yielding the keys avoids creating unnecessary data structures
    # and happily works with both python2 and python3 where the
    # .keys() method is a dictionary_view in python3 and a list in python2.
    def _keys(self):
        yielded = set()
        for k in self.keys():
            yielded.add(k)
            yield k
        if self.parent:
            for k in self.parent._keys():
                if k not in yielded:
                    yielded.add(k)
                    yield k

View on GitHub (pinned to 2661c7c0ef)

Solutions

  1. Use .get(key, default) instead of [key] when reading optional style attributes from StyleDict
  2. Provide inline style attributes (style="border: ...") on the table HTML you feed to tablepyxl
  3. Seed a root StyleDict with defaults for the keys your code reads so every lookup resolves via the parent chain

Example fix

# before
border = cell_style['border-top-width']  # KeyError: ... not found

# after
border = cell_style.get('border-top-width', '1pt')
Defensive patterns

Strategy: type-guard

Validate before calling

value = style_dict.get('border-top-width')
if value is None:
    value = '1pt'  # document your default instead of relying on KeyError

Type guard

def has_style_key(style_dict, key: str) -> bool:
    d = style_dict
    while d is not None:
        if key in d:
            return True
        d = getattr(d, 'parent', None)
    return False

Try / catch

try:
    val = style_dict[key]
except KeyError:
    val = DEFAULT_STYLE.get(key)  # never let StyleDict misses crash the render

Prevention

When it happens

Trigger: Calling style_dict['border'] (or similar key) on a StyleDict built from a <td> whose style chain has no such key — typically when parsing HTML without inline styles or when internal code requests an optional attribute directly with [] instead of .get().

Common situations: Feeding HTML tables that use CSS classes/stylesheets instead of inline style attributes (tablepyxl only reads inline styles); a parent style chain that was never initialized; custom forks querying style keys that upstream code never populates.

Related errors


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