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-docxrequestsbeautifulsoup4cssutils
许可证
本项目采用 MIT 许可证,详情请参见 LICENSE 文件。
报告问题
如有问题或建议,请通过 GitHub Issues 联系我们。
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
h2d-0.1.1.tar.gz
(6.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
h2d-0.1.1-py3-none-any.whl
(6.9 kB
view details)
File details
Details for the file h2d-0.1.1.tar.gz.
File metadata
- Download URL: h2d-0.1.1.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
442f4bd2cca744cdaafe2efa73e0a31f7ec6467fbe5b45d0e1db3397e99e06f0
|
|
| MD5 |
a588de8d2ff96c205fb204e36c99319c
|
|
| BLAKE2b-256 |
ce1ddb564d0e3c3a5c78b38468222bedbedc77835ef4617e834386d2be1c1fd8
|
File details
Details for the file h2d-0.1.1-py3-none-any.whl.
File metadata
- Download URL: h2d-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c54a358fe367e81abb2d58c773395c6899e8dae1799323b41ff0dce7200b6afe
|
|
| MD5 |
05c64d81357ac32726b09a2b7b62cacd
|
|
| BLAKE2b-256 |
c0beac8cc603c3389a22162fdd27a38196c5ff2ec94630eb3857a7dd175e24c8
|