轻松将您的html转为docx格式
Project description
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 联系我们。
Project details
Release history Release notifications | RSS feed
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.0.tar.gz
(8.2 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.0-py3-none-any.whl
(6.9 kB
view details)
File details
Details for the file h2d-0.1.0.tar.gz.
File metadata
- Download URL: h2d-0.1.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98d36af7edec82c67cee849f55543a3b0b08aa788d8d0fc69aa55e853d206f08
|
|
| MD5 |
af2e6b6c7b592d1646e9b648129652a6
|
|
| BLAKE2b-256 |
73b36561b995dc8558fd97aa5ca9700f09bc61760d870e3e98a11805592d295f
|
File details
Details for the file h2d-0.1.0-py3-none-any.whl.
File metadata
- Download URL: h2d-0.1.0-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 |
80ade7f25540ecd6725745a4d9d94f7b64dc74c6a658d0a9a23c93193ec46dbf
|
|
| MD5 |
90df91283d800bf60fef5a5fba1b63be
|
|
| BLAKE2b-256 |
9e5d83d911b36958fa7c85e1a4f487584c6b40843ce697c7c6905b7961b383f0
|