doc2txt
一个用于从Microsoft Word文档中提取文本的Python包,基于antiword工具构建,内置了跨平台的二进制文件和数据文件。
功能特性
- 从.doc格式的Microsoft Word文档中提取纯文本
- 跨平台支持(Windows、Linux、macOS ARM64)
- 内置antiword二进制文件,无需额外安装
- 文本格式优化功能,自动处理换行和表格
- 简单易用的Python API
支持的平台
- Windows (AMD64)
- Linux (AMD64)
- macOS (ARM64/Apple Silicon)
注意:macOS Intel (x86_64) 暂不支持
安装
pip install doc2txt
快速开始
基本用法
from doc2txt import extract_text
# 从Word文档提取文本
text = extract_text('document.doc')
print(text)
启用文本格式优化
from doc2txt import extract_text
# 提取文本并优化格式(合并断行,处理表格)
text = extract_text('document.doc', optimize_format=True)
print(text)
使用文本优化工具
from doc2txt import extract_text, optimize_text
# 先提取原始文本
raw_text = extract_text('document.doc')
# 手动优化文本格式
optimized_text = optimize_text(raw_text)
print(optimized_text)
API 参考
extract_text(doc_path, optimize_format=False)
从Microsoft Word文档中提取文本。
参数:
doc_path(str): .doc文件的路径optimize_format(bool): 是否优化文本格式,默认为False
返回:
str: 从文档中提取的文本内容
异常:
FileNotFoundError: 文件不存在ValueError: 文件格式不支持(仅支持.doc格式)RuntimeError: 平台不支持或二进制文件缺失、文档解析失败
optimize_text(text)
优化从文档中提取的文本格式。
参数:
text(str): 从文档中提取的原始文本
返回:
str: 格式优化后的文本
文本优化功能
文本优化功能解决了从Word文档提取文本时常见的格式问题:
- CJK检测: 使用字符范围识别中日韩(CJK)文本
- 换行合并: 自动合并没有缩进的连续行,保持段落的完整性
- CJK优化: 中日韩文本合并时不添加空格,其他语言添加空格
- 表格处理: 智能识别表格行(包含
|分隔符),保持表格格式 - 段落识别: 智能识别段落开头、列表项、标题等结构
- 空格处理: 移除行首多余空格,保持文档的清洁格式
项目结构
doc2txt/
├── __init__.py # 包的主入口
├── antiword_wrapper.py # antiword工具的Python封装
├── text_optimizer.py # 文本格式优化工具
├── bin/ # 跨平台二进制文件
│ ├── darwin-arm64/
│ ├── linux-amd64/
│ └── win-amd64/
└── antiword_share/ # antiword数据文件
├── fontnames
└── UTF-8.txt # UTF-8输出映射文件
依赖要求
无第三方 Python 依赖。
许可证
MIT License
贡献
欢迎提交Issue和Pull Request来改进这个项目。
更新日志
1.0.9
- 固定antiword UTF-8输出,修复非UTF-8环境中文变问号
- 移除第三方运行时依赖和无用编码映射文件
- 清理仓库生成物和测试输出文件
1.0.8
- 使用内置CJK字符检测,移除第三方语言检测依赖
- 改进CJK语言检测准确性
- 增强错误处理和输入验证
- 固定antiword UTF-8输出,避免非UTF-8环境中文变成问号
- 添加完整的测试套件
1.0.6
- 更新版本号用于PyPI发布
1.0.5
- 改进文本优化逻辑
1.0.0
- 初始版本发布
- 支持从.doc文件提取文本
- 内置跨平台antiword二进制文件
- 文本格式优化功能
Metadata
Release files for doc2txt 1.0.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| doc2txt-1.0.9.tar.gz | 315.6 kB | With provenance | Details for doc2txt-1.0.9.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| doc2txt-1.0.9-py3-none-any.whl | Python 3 | none | any | Details for doc2txt-1.0.9-py3-none-any.whl |
Total release size: 627.1 kB
Release files / doc2txt-1.0.9.tar.gz
| Download URL | doc2txt-1.0.9.tar.gz |
|---|---|
| Size | 315.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
067da8d5e0159f00605db6aa037a2d4df7513573af383a048ac948c24edca6fc
|
|
BLAKE2b-256 checksum How to use checksums |
1e14ca724acc7dfa5c74077adc282572c7dcf0e8e91c4b808575c783382a2ab3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.
Transparency logRelease files / doc2txt-1.0.9-py3-none-any.whl
| Download URL | doc2txt-1.0.9-py3-none-any.whl |
|---|---|
| Size | 311.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b5ff30f9c9f3a63ab37f86b7c076bb50926fb1b1370682124a7fbee4dd4af5cf
|
|
BLAKE2b-256 checksum How to use checksums |
e3cf594473e5a0c42f43c4e8fe6c0edd3ef029c979055152e7e263ce295f84c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.
Transparency logSecurity for doc2txt 1.0.9
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for doc2txt-1.0.9.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.
Transparency logA 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.