Skip to main content

H2D - HTML to DOCX Converter

H2D 是一个 Python 包,用于将 HTML 内容转换为 DOCX 文档,支持常见的 HTML 标签和图片下载,并允许用户设置全局字体。

功能特性

  • 支持常见的 HTML 标签,如 <h1> 到 <h6>、<p>、<img>、<a>、<ul>、<ol>、<table> 等。
  • 自动下载并嵌入 <img> 和 <a> 标签中的图片。
  • 允许通过 setFont 函数设置全局字体,支持中文字体。
  • 支持解析内联 CSS 样式,如字体大小、颜色等。

安装

您可以通过 PyPI 安装 H2D 包:

pip install h2d

使用示例

以下是一个简单的使用示例,展示如何将 HTML 转换为 DOCX,并设置全局字体为“宋体”:

import h2d

# 设置全局字体(可选)
h2d.setFont('宋体')

# 定义 HTML 字符串
html = """
<h1>一级标题(中文)</h1>
<h2>二级标题(中文)</h2>
<p>这是一个段落,包含 <strong>粗体</strong> 和 <em>斜体</em>。</p>
<img src="https://example.com/image.jpg" alt="示例图片" />
<ul>
    <li>列表项 1</li>
    <li>列表项 2</li>
</ul>
<table>
    <tr><th>表头</th></tr>
    <tr><td>单元格</td></tr>
</table>
"""

# 转换为 DOCX 并保存
docx = h2d.convert(html)
docx.save("output.docx")

使用文档

setFont(font_name_or_path)

设置全局字体。参数可以是字体名称(如 'Arial'、'宋体')或字体文件路径。

convert(html_string)

将 HTML 字符串转换为 DOCX Document 对象。

依赖库

  • python-docx
  • requests
  • beautifulsoup4
  • cssutils

许可证

本项目采用 MIT 许可证,详情请参见 LICENSE 文件。

报告问题

如有问题或建议,请通过 GitHub Issues 联系我们。

Metadata

Release files for h2d 0.1.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 h2d 0.1.1
File Size Uploaded Provenance Actions
h2d-0.1.1.tar.gz 6.6 kB Without provenance Details for h2d-0.1.1.tar.gz

Built distribution (wheel)

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

Total release size: 13.5 kB

Release files / h2d-0.1.1.tar.gz

Download URL h2d-0.1.1.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
442f4bd2cca744cdaafe2efa73e0a31f7ec6467fbe5b45d0e1db3397e99e06f0
BLAKE2b-256 checksum
How to use checksums
ce1ddb564d0e3c3a5c78b38468222bedbedc77835ef4617e834386d2be1c1fd8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.11

Release files / h2d-0.1.1-py3-none-any.whl

Download URL h2d-0.1.1-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c54a358fe367e81abb2d58c773395c6899e8dae1799323b41ff0dce7200b6afe
BLAKE2b-256 checksum
How to use checksums
c0beac8cc603c3389a22162fdd27a38196c5ff2ec94630eb3857a7dd175e24c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.11

Security for h2d 0.1.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.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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