DOCX Toolkit
A complete toolkit for processing Microsoft Word documents (.docx and legacy .doc formats).
Capabilities
1. Text + Table Extraction (.docx)
python3 {baseDir}/scripts/extract_text.py input.docx output.txt
Extracts all paragraphs and tables with structure preserved. Tables are formatted as pipe-delimited rows for easy parsing.
2. Text Extraction (Legacy .doc)
python3 {baseDir}/scripts/extract_doc_text.py input.doc output.txt
Handles legacy OLE2 .doc format using olefile. Extracts Unicode text from the WordDocument stream.
3. Image Extraction (.docx)
python3 {baseDir}/scripts/extract_images.py input.docx output_dir/
Extracts all embedded images with:
- - Automatic deduplication (MD5 hash comparison)
- Size filtering (skips tiny icons <5KB by default)
- Sequential renaming (img001.png, img002.jpg, etc.)
4. Image Compression
python3 {baseDir}/scripts/resize_images.py input_dir/ output_dir/ [--max-width 1024]
Batch resize/compress images for API processing (saves 50-70% on vision API costs).
Dependencies
- - Python 3.6+
- INLINECODE0 — for .docx processing
- INLINECODE1 — for legacy .doc processing
- INLINECODE2 — for image resizing (optional, only needed for resize script)
Install:
CODEBLOCK4
Use Cases
- - Document analysis: Extract text for AI review/summarization
- Migration: Pull content from Word docs into other formats
- Image audit: Extract and review all embedded images
- Cost optimization: Compress images before sending to vision APIs
- Batch processing: Process multiple documents in a pipeline
Notes
- - Large .doc files (>200MB) may require significant RAM for olefile processing
- Image extraction preserves original format (png/jpg/gif/etc.)
- Deduplication catches exact duplicates; near-duplicates still pass through
- CJK (Chinese/Japanese/Korean) text is fully supported in both extractors
DOCX 工具包
用于处理 Microsoft Word 文档(.docx 和旧版 .doc 格式)的完整工具包。
功能
1. 文本+表格提取(.docx)
bash
python3 {baseDir}/scripts/extract_text.py input.docx output.txt
提取所有段落和表格,并保留结构。表格格式化为管道分隔行,便于解析。
2. 文本提取(旧版 .doc)
bash
python3 {baseDir}/scripts/extract
doctext.py input.doc output.txt
使用 olefile 处理旧版 OLE2 .doc 格式。从 WordDocument 流中提取 Unicode 文本。
3. 图像提取(.docx)
bash
python3 {baseDir}/scripts/extract
images.py input.docx outputdir/
提取所有嵌入图像,具备以下功能:
- - 自动去重(MD5 哈希比较)
- 尺寸过滤(默认跳过小于 5KB 的小图标)
- 顺序重命名(img001.png、img002.jpg 等)
4. 图像压缩
bash
python3 {baseDir}/scripts/resize
images.py inputdir/ output_dir/ [--max-width 1024]
批量调整/压缩图像以用于 API 处理(可节省 50-70% 的视觉 API 成本)。
依赖项
- - Python 3.6+
- python-docx — 用于 .docx 处理
- olefile — 用于旧版 .doc 处理
- Pillow — 用于图像调整大小(可选,仅调整大小脚本需要)
安装:
bash
pip3 install python-docx olefile Pillow
使用场景
- - 文档分析:提取文本供 AI 审查/总结
- 迁移:从 Word 文档提取内容到其他格式
- 图像审计:提取并审查所有嵌入图像
- 成本优化:在发送到视觉 API 前压缩图像
- 批量处理:在流水线中处理多个文档
注意事项
- - 大型 .doc 文件(>200MB)可能需要大量 RAM 用于 olefile 处理
- 图像提取保留原始格式(png/jpg/gif 等)
- 去重功能捕获完全相同的副本;近似副本仍会通过
- 两个提取器均完全支持中日韩(CJK)文本