Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
02b7963
Made requested changes for pull request.
raithedavion Nov 24, 2025
534d80b
Re-added class and tag override functionality on top of release/1.1.1.
raithedavion Nov 24, 2025
9390836
Merge branch 'css-classes-tags/replay' of https://github.com/raitheda…
raithedavion Nov 30, 2025
b4e005a
Fixed text_decoration tests
raithedavion Nov 30, 2025
8dd6b71
Merge branch 'css-classes-tags/replay' of https://github.com/raitheda…
raithedavion Nov 30, 2025
a7e54d0
Fixed text-decoration
raithedavion Nov 30, 2025
e8edabc
Merge branch 'css-classes-tags/replay' of https://github.com/raitheda…
raithedavion Nov 30, 2025
fe94361
Fixed implemtnations & tests.
raithedavion Nov 30, 2025
f632d15
Merge branch 'css-classes-tags/replay' of https://github.com/raitheda…
raithedavion Dec 1, 2025
43f8cd6
Merge pull request #10 from raithedavion/css-classes-tags/replay
raithedavion Dec 1, 2025
738dc89
Updated history & readme.
raithedavion Dec 1, 2025
1ed7239
Merge branch 'css-classes-tags/replay' of https://github.com/raitheda…
raithedavion Dec 1, 2025
774c767
Merge pull request #11 from raithedavion/css-classes-tags/replay
raithedavion Dec 1, 2025
9924b37
Merge pull request #8 from raithedavion/main
raithedavion Dec 1, 2025
ed72b8c
Merge branch 'release/1.1.2' into class-map-tag-override
raithedavion Dec 2, 2025
efa1e96
Removed unused methods (migrated class and tag
raithedavion Dec 2, 2025
d832aea
Merge pull request #12 from raithedavion/main
raithedavion Dec 2, 2025
1a751c5
Minor updates.
raithedavion Dec 2, 2025
8d22278
Minor updates. Moved two methods to utils.
raithedavion Dec 4, 2025
06cddc0
Merge branch 'main' of https://github.com/raithedavion/html4docx
raithedavion Dec 4, 2025
4e0101e
Fixed utils.parse_text_decoration references.
raithedavion Dec 4, 2025
fd9a244
Merge pull request #13 from raithedavion/main
raithedavion Dec 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ Release History
---------------

1.1.2 (2025-12-xx)
++++++++++++++++++

**Updates**

- None
-

**Fixes**

- None
-

**New Features**

- None

- Added support for custom css class to word style mappings
- Added support for html tag to style overrides
- Added support for setting default word style for new documents.
- Added support for "!important" style precedence.

1.1.1 (2025-11-26)
++++++++++++++++++
Expand Down
96 changes: 95 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ All table styles we support can be found [here](https://python-docx.readthedocs.
There is 5 options that you can use to personalize your execution:
- Disable Images: Ignore all images.
- Disable Tables: If you do it, it will render just the raw tables content
- Disable Styles: Ignore all CSS styles.
- Disable Styles: Ignore all CSS styles. Also disables Style-Map.
- Disable Fix-HTML: Use BeautifulSoap to Fix possible HTML missing tags.
- Disable Style-Map: Ignore CSS classes to word styles mapping
- Disable Tag-Override: Ignore html tag overrides.
- Disable HTML-Comments: Ignore all "<!-- ... -->" comments from HTML.

This is how you could disable them if you want:
Expand All @@ -138,9 +140,99 @@ parser.options['tables'] = False # Default True
parser.options['styles'] = False # Default True
parser.options['fix-html'] = False # Default True
parser.options['html-comments'] = False # Default False
parser.options['style-map'] = False # Default True
parser.options['tag-override'] = False # Default True
docx = parser.parse_html_string(input_html_file_string)
```

## Extended Styling Features

### CSS Class to Word Style Mapping

Map HTML CSS classes to Word document styles:

```python
from html4docx import HtmlToDocx

style_map = {
'code-block': 'Code Block',
'numbered-heading-1': 'Heading 1 Numbered',
'finding-critical': 'Finding Critical'
}

parser = HtmlToDocx(style_map=style_map)
parser.add_html_to_document(html, document)
```

### Tag Style Overrides

Override default tag-to-style mappings:

```python
tag_overrides = {
'h1': 'Custom Heading 1', # All <h1> use this style
'pre': 'Code Block' # All <pre> use this style
}

parser = HtmlToDocx(tag_style_overrides=tag_overrides)
```

### Default Paragraph Style

Set custom default paragraph style:

```python
# Use 'Body' as default (default behavior)
parser = HtmlToDocx(default_paragraph_style='Body')

# Use Word's default 'Normal' style
parser = HtmlToDocx(default_paragraph_style=None)
```

### Inline CSS Styles

Full support for inline CSS styles on any element:

```html
<p style="color: red; font-size: 14pt">Red 14pt paragraph</p>
<span style="font-weight: bold; color: blue">Bold blue text</span>
```

Supported CSS properties:

- color
- font-size
- font-weight (bold)
- font-style (italic)
- text-decoration (underline, line-through)
- font-family
- text-align
- background-color
- Border (for tables)
- Verticial Align (for tables)

### !important Flag Support

Proper CSS precedence with !important:

```html
<span style="color: gray">
Gray text with <span style="color: red !important">red important</span>.
</span>
```

The !important flag ensures highest priority.

### Style Precedence Order

Styles are applied in this order (lowest to highest priority):

1. Base HTML tag styles (`<b>`, `<em>`, `<code>`)
2. Parent span styles
3. CSS class-based styles (from `style_map`)
4. Inline CSS styles (from `style` attribute)
5. !important inline CSS styles (highest priority)

#### Metadata

You're able to read or set docx metadata:
Expand Down Expand Up @@ -204,6 +296,8 @@ My goal in forking and fixing/updating this package was to complete my current t
- Being able to use inline images on same paragraph. | [Dfop02](https://github.com/dfop02)
- Refactory Tests to be more consistent and less 'human validation' | [Dfop02](https://github.com/dfop02)
- Support for common CSS properties for text | [Lynuxen](https://github.com/Lynuxen)
- Support for CSS classes to Word Styles | [raithedavion](https://github.com/raithedavion)
- Support for HTML tag style overrides | [raithedavion](https://github.com/raithedavion)

## Known Issues

Expand Down
9 changes: 8 additions & 1 deletion html4docx/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
'images': True,
'tables': True,
'styles': True,
'html-comments': False
'html-comments': False,
"style-map": True,
"tag-override": True,
}

DEFAULT_TABLE_ROW_SELECTORS = [
Expand Down Expand Up @@ -63,6 +65,11 @@
'double': WD_UNDERLINE.DOUBLE,
}

# NEW DEFAULTS
DEFAULT_STYLE_MAP = dict()
DEFAULT_TAG_OVERRIDES = dict()
DEFAULT_PARAGRAPH_STYLE = "Normal"

STYLES = {
'ul': 'List Bullet',
'ul2': 'List Bullet 2',
Expand Down
Loading