Skip to main content

html3docx

A fork of https://github.com/pqzx/html2docx. This version will focus on expedient changes for our particular use case, and thus will receive infrequent updates.

Dependencies: python-docx & bs4

To install

pip install html3docx

PyPI: https://pypi.org/project/html3docx/

Improvements

  • Fix for KeyError when handling an img tag without a src attribute.
  • Images with a width attribute will be scaled according to that width.
  • Fix for AttributeError when handling a leading br tag, either at the top of the HTML snippet, or within a td or th cell.
  • Fix for IndexError when a table has more cells in latter rows than in the first row.
  • Ordered lists will now restart at 1. when proceeded by a paragraph that is not a numbered list.
  • Parameterized image fetcher function.
  • Parameterized default styles for OL, UL, and TABLE tags.
  • Fix for KeyError when handling an anchor with no href attribute.
  • Added support for images with base64 encoded data src.

Original README

Usage

Add strings of html to an existing docx.Document object

from docx import Document
from htmldocx import HtmlToDocx

document = Document()
new_parser = HtmlToDocx()
# do stuff to document

html = '<h1>Hello world</h1>'
new_parser.add_html_to_document(html, document)

# do more stuff to document
document.save('your_file_name')

Convert files directly

from htmldocx import HtmlToDocx

new_parser = HtmlToDocx()
new_parser.parse_html_file(input_html_file_path, output_docx_file_path)

Convert files from a string

from htmldocx import HtmlToDocx

new_parser = HtmlToDocx()
docx = new_parser.parse_html_string(input_html_file_string)

Change table styles

Tables are not styled by default. Use the table_style attribute on the parser to set a table style. The style is used for all tables.

from htmldocx import HtmlToDocx

new_parser = HtmlToDocx()
new_parser.table_style = 'Light Shading Accent 4'

To add borders to tables, use the TableGrid style:

new_parser.table_style = 'TableGrid'

Default table styles can be found here: https://python-docx.readthedocs.io/en/latest/user/styles-understanding.html#table-styles-in-default-template

Change default paragraph style

No style is applied to the paragraphs by default. Use the paragraph_style attribute on the parser to set a default paragraph style. The style is used for all paragraphs. If additional styling ( color, background color, alignment...) is defined in the HTML, it will be applied after the paragraph style.

from htmldocx import HtmlToDocx

new_parser = HtmlToDocx()
new_parser.paragraph_style = 'Quote'

Default paragraph styles can be found here: https://python-docx.readthedocs.io/en/latest/user/styles-understanding.html#paragraph-styles-in-default-template

Metadata

Release files for html3docx 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for html3docx 1.0.1
File Size Uploaded Provenance Actions
html3docx-1.0.1.tar.gz 13.7 kB Without provenance Details for html3docx-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for html3docx 1.0.1
File Interpreter ABI Platform Actions
html3docx-1.0.1-py3-none-any.whl Python 3 none any Details for html3docx-1.0.1-py3-none-any.whl

Total release size: 25.6 kB

Release files / html3docx-1.0.1.tar.gz

Download URL html3docx-1.0.1.tar.gz
Size 13.7 kB
Tags Source
SHA-256 checksum
How to use checksums
76a8e19e40f2853bfe28b62584a7e6a0f2ea35c8ed178752a3c408e7dd70ae18
BLAKE2b-256 checksum
How to use checksums
5b469ee3ae288e440dba2372454c5c5e69d97a156ba5f83c1368d896c18e24a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.6

Release files / html3docx-1.0.1-py3-none-any.whl

Download URL html3docx-1.0.1-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7b835a643e006ad3e712d7b9c68092ba702e8a3a920ce698293f6fdb8ee28128
BLAKE2b-256 checksum
How to use checksums
d010d78037b63b42c575edb0fd70c7a2dcd72b93263623c5923dbd765aeab4d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.6

Security for html3docx 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page