Skip to main content

A fork of https://github.com/pqzx/html2docx. This version will focus on expedient changes for our particular use case.

Project description

html3docx

A fork of https://github.com/pqzx/html2docx. This version will focus on expedient changes for our particular use case.

Dependencies: python-docx & bs4

To install

pip install 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.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

html3docx-0.0.9.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

html3docx-0.0.9-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file html3docx-0.0.9.tar.gz.

File metadata

  • Download URL: html3docx-0.0.9.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for html3docx-0.0.9.tar.gz
Algorithm Hash digest
SHA256 1ba0b3ea700f7623ed2ae12ee98705ecc6f0ab6e75a59df152ea42eb0cd63150
MD5 674d95b70f428cdbf0aec0fc13b4c60e
BLAKE2b-256 f97e951be1a3365ec56cbaf4ee171cd52888b41450aa578b5ed3cc4ce79d98a8

See more details on using hashes here.

File details

Details for the file html3docx-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: html3docx-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for html3docx-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 413118a612262251487a14d036da6cf7388c5c5554830ccac349ec0f4f23b460
MD5 749b6fe487d7b14c7ab6a9f571f04299
BLAKE2b-256 83ab1ccfdcf887240bd284b3c3313bbebad25cacba39827a0956837250eb67df

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page