You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<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).
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.
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.
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.
@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.
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.
Activity
dfop02 commentedon Feb 18, 2026
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.
dfop02 commentedon Feb 22, 2026
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 passtag_style_overrides, the code inh4d.pychecks 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.
chubbardbv commentedon Feb 22, 2026
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.
The
tag_overridesreference the custom style names created in template.docx.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.
dfop02 commentedon Feb 22, 2026
@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:
Thanks again for the support and for helping improve the project.
chubbardbv commentedon Feb 23, 2026
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!