Skip to content

Tag Overrides Not Working for <code> #62

Closed

Description

@chubbardbv

I have 2 styles defined in Word that I want to use for code blocks and inline code and have defined them as tag overrides:

tag_overrides = {
  'code': 'Inline Code',
  'pre': 'Code Block'
}
parser = HtmlToDocx(tag_style_overrides=tag_overrides)

<pre> is correctly styled with the 'Code Block' word style, however, the <code> override seems to be ignored. What is rendered in place is Courier 10pt font (default) even though the 'Inline Code' style defines a different font, background color, and border. I have made sure that 'Inline Code' is a character style (not a paragraph style).

Activity

  1. self-assigned this
    on Feb 18, 2026
  2. dfop02 commented on Feb 18, 2026

    @dfop02
    Owner

    Hi @chubbardbv , thanks for reporting the issue.
    I'm busy this week, but I'm planning to take care of all PR next weekend, including this one.

  3. dfop02 commented on Feb 22, 2026

    @dfop02
    Owner

    Hey @chubbardbv!

    I did some tests to better understand your context.

    From what I see, you're using custom styles created directly in Word, right? The important part is that when you initialize Html4Docx() and pass tag_style_overrides, the code in h4d.py checks if the style actually exists in the current document (basically something like 'CustomStyle' in document.styles).

    By default, when you create a new document with python-docx (Document()), it only contains the built-in default styles from the standard template. Your custom Word styles won’t exist there automatically unless you add or import from template.docx.

    Could you share the code you're using to generate the .docx? That would help me understand your flow better and understand what are you expecting.

  4. chubbardbv commented on Feb 22, 2026

    @chubbardbv
    Author

    Yes, I am using styles created in a template.docx Word document. I am creating the document with python-docx, but I am initializing it with this template document.

    doc = docx.Document('template.docx')
    

    The tag_overrides reference the custom style names created in template.docx.

    tag_overrides = {
      'code': 'Inline Code',
      'pre': 'Code Block'
    }
    parser = HtmlToDocx(tag_style_overrides=tag_overrides)
    

    Honestly, a better solution for this would be to directly handle a CSS file linked in the original HTML, but in the meantime, this is my workaround. I see your code supports inline styles defined in the HTML so I may explore this route as well.

  5. dfop02 commented on Feb 22, 2026

    @dfop02
    Owner

    @chubbardbv I pushed a fix in PR #65, could you take a look and let me know if it solves your issue?

    Regarding <style> tag support and importing external CSS files: I’ve actually started working on that on PR #57. It’s a more complex feature than it sounds because it needs to harmonize with the existing parsing logic without breaking current behavior. There are quite a few edge cases to consider, so I’m moving carefully. I’ve been working on it on weekends, and hopefully I’ll have something ready for testing soon.

    For now, you still have a few solid alternatives:

    • Use inline CSS
    • Programmatically replicate your styles using python-docx
    • Or continue loading your template with the custom styles (especially if the fix works for you)

    Thanks again for the support and for helping improve the project.

  6. chubbardbv commented on Feb 23, 2026

    @chubbardbv
    Author

    I have tested and this fixes the issue!

    Unfortunately when uploading the document to Sharepoint, all formatting is lost, but I have realized this is a limitation of python-docx and has nothing to do with this package.

    Thank you for your work on this!

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions