Python wrapper for antiword with bundled binary and data files
Project description
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: 从文档中提取的文本内容
异常:
RuntimeError: 平台不支持或二进制文件缺失subprocess.CalledProcessError: antiword执行失败
optimize_text(text)
优化从文档中提取的文本格式。
参数:
text(str): 从文档中提取的原始文本
返回:
str: 格式优化后的文本
文本优化功能
文本优化功能解决了从Word文档提取文本时常见的格式问题:
- 换行合并: 自动合并没有缩进的连续行,保持段落的完整性
- 表格处理: 智能识别表格行(包含
|分隔符),保持表格格式 - 空格处理: 移除行首多余空格,保持文档的清洁格式
项目结构
doc2txt/
├── __init__.py # 包的主入口
├── antiword_wrapper.py # antiword工具的Python封装
├── text_optimizer.py # 文本格式优化工具
├── bin/ # 跨平台二进制文件
│ ├── darwin-arm64/
│ ├── linux-amd64/
│ └── win-amd64/
└── antiword_share/ # antiword数据文件
├── fontnames
└── *.txt # 字符编码映射文件
依赖要求
本包无外部依赖,所有必需的工具和数据文件都已内置。
许可证
MIT License
贡献
欢迎提交Issue和Pull Request来改进这个项目。
更新日志
1.0.0
- 初始版本发布
- 支持从.doc文件提取文本
- 内置跨平台antiword二进制文件
- 文本格式优化功能
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
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
File details
Details for the file doc2txt-1.0.5.tar.gz.
File metadata
- Download URL: doc2txt-1.0.5.tar.gz
- Upload date:
- Size: 350.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37cb985e739f995c09ee43e5a150ab7fa85f2f54888ec8d41e26f60111c1e9f1
|
|
| MD5 |
fb1bb05ad1ba11eee6f8ae9c040ad813
|
|
| BLAKE2b-256 |
d6e1a8fc23d314327bb1e870d0a8f7afa528da53e33f7149e2b3cc7a98d39c4b
|
Provenance
The following attestation bundles were made for doc2txt-1.0.5.tar.gz:
Publisher:
publish.yml on Quantatirsk/doc2txt-pypi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
doc2txt-1.0.5.tar.gz -
Subject digest:
37cb985e739f995c09ee43e5a150ab7fa85f2f54888ec8d41e26f60111c1e9f1 - Sigstore transparency entry: 280790995
- Sigstore integration time:
-
Permalink:
Quantatirsk/doc2txt-pypi@9cc3bb9801148d64e9ce30e33339600466bb75ae -
Branch / Tag:
refs/tags/v1.0.5 - Owner: https://github.com/Quantatirsk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9cc3bb9801148d64e9ce30e33339600466bb75ae -
Trigger Event:
release
-
Statement type:
File details
Details for the file doc2txt-1.0.5-py3-none-any.whl.
File metadata
- Download URL: doc2txt-1.0.5-py3-none-any.whl
- Upload date:
- Size: 412.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
442f14045c5367b028368490d1c9e116c56eb2607249e85e48f44a81f0c8b6e2
|
|
| MD5 |
348f72fea6c88478421f2ad203c53ea7
|
|
| BLAKE2b-256 |
cefa8959ab82abb6c9b8ce626f47911a0b60793eeb7bff859014245f576e78a1
|
Provenance
The following attestation bundles were made for doc2txt-1.0.5-py3-none-any.whl:
Publisher:
publish.yml on Quantatirsk/doc2txt-pypi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
doc2txt-1.0.5-py3-none-any.whl -
Subject digest:
442f14045c5367b028368490d1c9e116c56eb2607249e85e48f44a81f0c8b6e2 - Sigstore transparency entry: 280791000
- Sigstore integration time:
-
Permalink:
Quantatirsk/doc2txt-pypi@9cc3bb9801148d64e9ce30e33339600466bb75ae -
Branch / Tag:
refs/tags/v1.0.5 - Owner: https://github.com/Quantatirsk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9cc3bb9801148d64e9ce30e33339600466bb75ae -
Trigger Event:
release
-
Statement type: