pdf2word-pro 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pdf2word_pro-0.1.0/LICENSE +21 -0
- pdf2word_pro-0.1.0/PKG-INFO +267 -0
- pdf2word_pro-0.1.0/README.md +232 -0
- pdf2word_pro-0.1.0/README_EN.md +232 -0
- pdf2word_pro-0.1.0/pyproject.toml +66 -0
- pdf2word_pro-0.1.0/setup.cfg +4 -0
- pdf2word_pro-0.1.0/src/pdf2word_pro/__init__.py +13 -0
- pdf2word_pro-0.1.0/src/pdf2word_pro/__main__.py +8 -0
- pdf2word_pro-0.1.0/src/pdf2word_pro/_version.py +1 -0
- pdf2word_pro-0.1.0/src/pdf2word_pro/cli.py +14 -0
- pdf2word_pro-0.1.0/src/pdf2word_pro/core.py +2537 -0
- pdf2word_pro-0.1.0/src/pdf2word_pro/py.typed +0 -0
- pdf2word_pro-0.1.0/src/pdf2word_pro.egg-info/PKG-INFO +267 -0
- pdf2word_pro-0.1.0/src/pdf2word_pro.egg-info/SOURCES.txt +16 -0
- pdf2word_pro-0.1.0/src/pdf2word_pro.egg-info/dependency_links.txt +1 -0
- pdf2word_pro-0.1.0/src/pdf2word_pro.egg-info/entry_points.txt +2 -0
- pdf2word_pro-0.1.0/src/pdf2word_pro.egg-info/requires.txt +8 -0
- pdf2word_pro-0.1.0/src/pdf2word_pro.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 wqzh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pdf2word-pro
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Convert PDF to Word (.docx) while preserving layout: paragraphs, tables, images, side-by-side figures, headers and footers.
|
|
5
|
+
Author: wqzh
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/wqzh/pdf2word
|
|
8
|
+
Project-URL: Repository, https://github.com/wqzh/pdf2word
|
|
9
|
+
Project-URL: Issues, https://github.com/wqzh/pdf2word/issues
|
|
10
|
+
Keywords: pdf,docx,word,pdf-to-word,pdf-to-docx,conversion,layout,pdfplumber,python-docx
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: End Users/Desktop
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Topic :: Office/Business :: Office Suites
|
|
22
|
+
Classifier: Topic :: Text Processing :: Markup
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.9
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Dist: pdfplumber>=0.10.0
|
|
28
|
+
Requires-Dist: python-docx>=1.0.0
|
|
29
|
+
Requires-Dist: pillow>=9.0.0
|
|
30
|
+
Requires-Dist: pypdf>=3.0.0
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: build>=1.2; extra == "dev"
|
|
33
|
+
Requires-Dist: twine>=5.0; extra == "dev"
|
|
34
|
+
Dynamic: license-file
|
|
35
|
+
|
|
36
|
+
[中文](https://github.com/wqzh/pdf2word/blob/main/README.md) | [English](https://github.com/wqzh/pdf2word/blob/main/README_EN.md)
|
|
37
|
+
|
|
38
|
+
# pdf2word-pro
|
|
39
|
+
|
|
40
|
+
A command-line tool that converts PDF into Word (`.docx`) while trying hard to **preserve the original layout**: paragraphs, fonts and sizes, tables (including merged cells and shading), images (including side-by-side figures and their captions), headers and footers, page breaks and page sizes.
|
|
41
|
+
|
|
42
|
+
A pure-Python implementation that only relies on `pdfplumber` for low-level PDF parsing and `python-docx` for assembling the Word document. After installation the command is `pdf2word-pro`; `python -m pdf2word_pro` also works, and the legacy `python pdf2docx.py` entry point is kept in the repository.
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## 1. Features
|
|
47
|
+
|
|
48
|
+
| Area | What it does |
|
|
49
|
+
| --- | --- |
|
|
50
|
+
| Text | Rebuilds text as character → line → paragraph, keeping font, size, bold/italic and colour; no extra spaces for CJK, de-hyphenation for English |
|
|
51
|
+
| Paragraphs | Decides soft vs. hard line breaks by "does the remaining space on the previous line fit the first word of the next line"; preserves alignment, first-line indent, and maps vertical spacing to Word space-before/space-after |
|
|
52
|
+
| Headings | Short paragraphs noticeably larger than body text are mapped to Word Heading 1/2/3 styles (easy to restyle afterwards) |
|
|
53
|
+
| Columns | Detects two-column layouts by locating the gutter and emits content column by column |
|
|
54
|
+
| Tables | Rebuilds the grid from PDF rules, including merged cells and cell shading; cell text is scaled to the original size; tables continuing across pages are stitched back together with duplicate header rows removed |
|
|
55
|
+
| Images | Prefers the original image stream (JPEG as-is, lossless RGB/grey); falls back to region rendering; keeps original size, indentation and centring |
|
|
56
|
+
| Side-by-side figures | Figures sharing a horizontal band (e.g. an a) b) c) d) grid, or two figures side by side) are rebuilt as a borderless table at their original column positions — never split or reordered |
|
|
57
|
+
| Inset images | Legends / magnified details overlaid on a larger figure are merged into the outer figure's rendered region instead of being mistaken for a second column |
|
|
58
|
+
| Vector graphics | Charts made of curves and filled shapes are clustered, rendered as a single image, and their internal text is removed to avoid duplication |
|
|
59
|
+
| Headers / footers | Repeating cross-page lines become Word headers/footers, with page numbers emitted as `PAGE` / `NUMPAGES` fields; an "without header/footer" copy can be exported as well |
|
|
60
|
+
| Pages | Page margins are derived from the original page size and content extents; mixed portrait/landscape pages produce separate sections |
|
|
61
|
+
| Robustness | Encrypted or damaged PDFs are rewritten through `pypdf` and retried; a page that fails to parse is rasterised as a fallback; illegal XML characters are stripped |
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## 2. Requirements
|
|
66
|
+
|
|
67
|
+
- Python **3.9+** (developed and verified on Python 3.11.9 / Windows 10)
|
|
68
|
+
- Dependencies (see `requirements.txt`):
|
|
69
|
+
|
|
70
|
+
| Package | Purpose | Verified version |
|
|
71
|
+
| --- | --- | --- |
|
|
72
|
+
| `pdfplumber` | Read PDF pages, characters, tables, rules and images | 0.11.4 |
|
|
73
|
+
| `python-docx` | Build the `.docx` file | 1.2.0 |
|
|
74
|
+
| `pillow` | Image decoding / PNG encoding | 13.0.0 |
|
|
75
|
+
| `pypdf` | Fallback rewrite of damaged or encrypted PDFs | 7.1.0 |
|
|
76
|
+
|
|
77
|
+
`concurrent.futures`, `argparse`, `json`, `logging`, etc. are part of the standard library and need no extra installation.
|
|
78
|
+
|
|
79
|
+
### Install
|
|
80
|
+
|
|
81
|
+
```shell
|
|
82
|
+
pip install pdf2word-pro
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
From source (development):
|
|
86
|
+
|
|
87
|
+
```shell
|
|
88
|
+
pip install -r requirements.txt
|
|
89
|
+
pip install -e .
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## 3. Quick start
|
|
95
|
+
|
|
96
|
+
```shell
|
|
97
|
+
pdf2word-pro input.pdf
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Without `-o`, the output is a `.docx` with the same name next to the input PDF, and its final path is printed to stdout.
|
|
101
|
+
|
|
102
|
+
```shell
|
|
103
|
+
pdf2word-pro input.pdf -o output.docx
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## 4. Command-line options
|
|
109
|
+
|
|
110
|
+
```text
|
|
111
|
+
usage: pdf2word-pro [-h] [-o OUTPUT] [--pages PAGES] [--password ***]
|
|
112
|
+
[--keep-page-breaks] [--no-columns] [--no-figures]
|
|
113
|
+
[--no-headings] [--no-header-footer]
|
|
114
|
+
[--remove-page-head-foot] [--no-fonts] [--dpi DPI]
|
|
115
|
+
[--cache-page] [--cache-dir CACHE_DIR] [--jobs JOBS] [-v]
|
|
116
|
+
pdf
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
| Option | Default | Description |
|
|
120
|
+
| --- | --- | --- |
|
|
121
|
+
| `pdf` | required | Input PDF path (positional) |
|
|
122
|
+
| `-o`, `--output` | same name as input, `.docx` | Output `.docx` path; missing directories are created |
|
|
123
|
+
| `--pages` | all pages | Convert only the given pages, 1-based. Accepts `1-3,5,8-` (`8-` means page 8 to the end); out-of-range values are clamped |
|
|
124
|
+
| `--password` | none | Password for an encrypted PDF |
|
|
125
|
+
| `--keep-page-breaks` | off | Insert a page break wherever the original PDF breaks a page (by default content reflows) |
|
|
126
|
+
| `--no-columns` | off | Disable two-column detection and read full-width lines |
|
|
127
|
+
| `--no-figures` | off | Do not render vector graphics as images (text and bitmaps only) |
|
|
128
|
+
| `--no-headings` | off | Do not apply Word heading styles; everything becomes body paragraphs |
|
|
129
|
+
| `--no-header-footer` | off | Do not extract headers/footers (their text stays in the body) |
|
|
130
|
+
| `--remove-page-head-foot` | off | Besides the full document, **also** save `<name>-no-head-foot.docx` without headers/footers/page numbers, and write the per-page detection result to the console and `<name>-page-head-foot.json` |
|
|
131
|
+
| `--no-fonts` | off | Do not keep original font names (use the document default) |
|
|
132
|
+
| `--dpi` | `200` | DPI used when rasterising regions (including the whole-page fallback); higher is sharper and larger |
|
|
133
|
+
| `--cache-page` | off | Dump each page's parse result (JSON + plain text + images) to disk for troubleshooting |
|
|
134
|
+
| `--cache-dir` | `_pdf2word_pages` next to the output | Directory for the per-page cache; created if missing |
|
|
135
|
+
| `--jobs` | `1` | Number of parsing processes; when `>1` and more than 8 pages, pages are parsed in parallel (pages are independent, so long documents get several times faster) |
|
|
136
|
+
| `-v`, `--verbose` | off | DEBUG-level logging, and re-raise the full traceback on failure |
|
|
137
|
+
|
|
138
|
+
Exit codes: `0` success, `1` conversion failed (the reason is logged; `-v` shows the full stack).
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## 5. Output files
|
|
143
|
+
|
|
144
|
+
Assuming `-o output.docx`:
|
|
145
|
+
|
|
146
|
+
| File | Produced when | Description |
|
|
147
|
+
| --- | --- | --- |
|
|
148
|
+
| `output.docx` | always | Full document, with headers/footers |
|
|
149
|
+
| `output-no-head-foot.docx` | with `--remove-page-head-foot` | Same document with headers/footers/page numbers removed |
|
|
150
|
+
| `output-page-head-foot.json` | with `--remove-page-head-foot` | Per-page header/footer/page-number detection result |
|
|
151
|
+
| `_pdf2word_pages\page_0001.json` … | with `--cache-page` | Structured parse result of that page (lines/characters/tables/image boxes, UTF-8) |
|
|
152
|
+
| `_pdf2word_pages\page_0001.txt` | with `--cache-page` | Plain-text view of that page (line text + table cell text) |
|
|
153
|
+
| `_pdf2word_pages\page_0001_*.jpg/png` | with `--cache-page` | Images / rendered vector figures extracted from that page |
|
|
154
|
+
| `_pdf2word_pages\manifest.json` | with `--cache-page` | Summary index of all pages (page number, block count, files, timing) |
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
## 6. Examples
|
|
159
|
+
|
|
160
|
+
### 6.1 Convert the whole document
|
|
161
|
+
|
|
162
|
+
```shell
|
|
163
|
+
pdf2word-pro input.pdf -o output.docx
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### 6.2 Convert only a few pages (most common for layout regression)
|
|
167
|
+
|
|
168
|
+
```shell
|
|
169
|
+
pdf2word-pro input.pdf -o sample.docx --pages 12-14,20
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### 6.3 Speed up a long document with multiple processes
|
|
173
|
+
|
|
174
|
+
```shell
|
|
175
|
+
pdf2word-pro input.pdf -o output-job8.docx --jobs 8
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### 6.4 Remove headers and footers (keeping the full version)
|
|
179
|
+
|
|
180
|
+
```shell
|
|
181
|
+
pdf2word-pro input.pdf -o output.docx --remove-page-head-foot
|
|
182
|
+
# produces: output.docx, output-no-head-foot.docx, output-page-head-foot.json
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### 6.5 Encrypted PDF
|
|
186
|
+
|
|
187
|
+
```shell
|
|
188
|
+
pdf2word-pro encrypted.pdf -o decrypted.docx --password "***"
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### 6.6 Keep original page breaks and disable column detection (layout inspection)
|
|
192
|
+
|
|
193
|
+
```shell
|
|
194
|
+
pdf2word-pro input.pdf -o output-layout.docx `
|
|
195
|
+
--keep-page-breaks --no-columns --cache-page --cache-dir _dbg -v
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### 6.7 Use it as a library
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
from pdf2word_pro import Config, pdf_to_docx
|
|
202
|
+
|
|
203
|
+
cfg = Config(pages={45, 46, 47}, image_dpi=300, jobs=4) # pages are 0-based
|
|
204
|
+
out = pdf_to_docx("input.pdf", "output.docx", cfg)
|
|
205
|
+
print(out)
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
`Config` fields mirror the command-line options one-to-one (see the comments next to the `Config` definition): `keep_page_breaks`, `detect_columns`, `render_figures`, `image_dpi`, `jobs`, `cache_pages`, `cache_dir`, `remove_page_head_foot`, and so on.
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## 7. How it works
|
|
213
|
+
|
|
214
|
+
1. **Per-page parsing** (`parse_page`, pdfplumber)
|
|
215
|
+
- Tables: `find_tables()` → grid + merged cells + shading + text/images inside cells
|
|
216
|
+
- Images: original image stream first, region rendering as a fallback; inset images (legends/details) are merged into the outer figure
|
|
217
|
+
- Vector graphics: curves and filled shapes are clustered, rendered as one image, and their internal text is removed
|
|
218
|
+
- Text: characters → lines → paragraphs (font/size/bold/italic/colour); two-column detection
|
|
219
|
+
2. **Global passes**
|
|
220
|
+
- Headers/footers: repeating cross-page lines are recognised and written as Word headers/footers, page numbers as fields
|
|
221
|
+
- Line-join: English de-hyphenation, no spaces for CJK
|
|
222
|
+
- Cross-page merging: tables (column-aligned, duplicate header rows removed) and paragraphs (last line full and next page not indented)
|
|
223
|
+
3. **Writing the docx** (`write_docx`)
|
|
224
|
+
- Page size/margins, alignment, indentation, line/paragraph spacing, table column widths and row heights, image size/alignment
|
|
225
|
+
- Side-by-side figures become a borderless table; a figure row is never split across pages and its last row stays with the caption
|
|
226
|
+
4. **Robustness**: encrypted/damaged PDFs, whole-page fallback on parse failure, illegal XML character filtering
|
|
227
|
+
|
|
228
|
+
---
|
|
229
|
+
|
|
230
|
+
## 8. How side-by-side figures are laid out
|
|
231
|
+
|
|
232
|
+
This is the most error-prone part of any PDF-to-Word conversion. Key decisions:
|
|
233
|
+
|
|
234
|
+
- **Row detection**: only figures inside the same horizontal band form a row; adjacent rows with different column counts are never merged, so figures cannot be mixed up.
|
|
235
|
+
- **One figure per column**: each figure gets its own column, and neighbouring figures are separated at the **midpoint** between their two edges. No horizontal cell merging is performed, so even if two figures slightly overlap in the PDF, they never end up squeezed into one merged cell.
|
|
236
|
+
- **Row height and pagination**: table rows are set to `cantSplit` (a row never breaks across pages) and the last row's paragraphs use `keep_with_next`, so a figure row stays on the same page as the caption that follows it (`a) … b) …` or `Figure x.x-x`).
|
|
237
|
+
- **Inset figures**: containment (area ratio < 90 % and fully enclosed) identifies "legend / magnified detail" images, which are merged into the outer figure's rendered region rather than treated as a second side-by-side column.
|
|
238
|
+
- **Alignment**: for centred rows the table indent follows the original table's left edge, and scaling is computed proportionally from the available width, so nothing is stretched.
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
## 9. Known limitations and troubleshooting
|
|
243
|
+
|
|
244
|
+
- **Scanned PDFs**: there is no text layer, so each page can only become an image (the log says "no content extracted (possibly a scan — run OCR first)"). Run OCR before converting.
|
|
245
|
+
- **Fonts missing ToUnicode**: you will see a "N glyphs could not be mapped to Unicode" warning; those characters can only be dropped or replaced.
|
|
246
|
+
- **Multiple images side by side inside a table cell**: currently emitted in vertical order; per-cell side-by-side layout is not implemented yet.
|
|
247
|
+
- **Figures combining a bitmap base with vector overlays**: if the base is a bitmap and the coloured areas are vector-drawn, taking the image stream may leave only the base; compare with `--no-figures`, or inspect the page with `--cache-page`.
|
|
248
|
+
- **Slow conversion**: add `--jobs 8`; to find a stuck page, isolate it with `--pages` and add `-v` to see stage timings.
|
|
249
|
+
- **Images too large / too small**: adjust `--dpi` (default 200). Higher DPI is sharper and produces a larger `.docx`.
|
|
250
|
+
|
|
251
|
+
---
|
|
252
|
+
|
|
253
|
+
## 10. Repository layout
|
|
254
|
+
|
|
255
|
+
```text
|
|
256
|
+
pyproject.toml packaging config (PEP 621 / setuptools)
|
|
257
|
+
LICENSE MIT license
|
|
258
|
+
requirements.txt dependency list
|
|
259
|
+
README.md Chinese documentation
|
|
260
|
+
README_EN.md this file
|
|
261
|
+
pdf2docx.py legacy entry point
|
|
262
|
+
src/pdf2word_pro/ package source
|
|
263
|
+
core.py converter implementation
|
|
264
|
+
cli.py command-line entry
|
|
265
|
+
__main__.py python -m entry
|
|
266
|
+
__init__.py public API
|
|
267
|
+
```
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
[中文](https://github.com/wqzh/pdf2word/blob/main/README.md) | [English](https://github.com/wqzh/pdf2word/blob/main/README_EN.md)
|
|
2
|
+
|
|
3
|
+
# pdf2word-pro
|
|
4
|
+
|
|
5
|
+
把 PDF 转成 Word(`.docx`)的命令行工具,目标是**尽量保留原 PDF 的版面**:段落、字体字号、表格(含合并单元格与底色)、图片(含并排插图与图注)、页眉页脚、分页与页面尺寸。
|
|
6
|
+
|
|
7
|
+
纯 Python 自研实现,只依赖 `pdfplumber` 做 PDF 底层解析、`python-docx` 做 Word 组装。安装后命令行入口为 `pdf2word-pro`,同时支持 `python -m pdf2word_pro`;仓库内仍保留 `python pdf2docx.py` 旧入口。
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## 1. 特性概览
|
|
12
|
+
|
|
13
|
+
| 类别 | 说明 |
|
|
14
|
+
| --- | --- |
|
|
15
|
+
| 文字 | 按「字符 → 行 → 段落」重建,保留字体、字号、粗体/斜体、颜色;中文不加空格、英文去连字符断词 |
|
|
16
|
+
| 段落 | 用「上一行右侧剩余空间能否放下下一行首个词」判断软换行还是真换行;保留对齐方式、首行缩进、行距/段间距(按原 PDF 的纵向间距换算成 Word 的段前/段后) |
|
|
17
|
+
| 标题 | 字号明显大于正文字号的短段落自动套用 Word 的「标题 1/2/3」样式(可在转换后统一改样式) |
|
|
18
|
+
| 双栏 | 自动检测双栏排版的中缝(gutter),按栏输出 |
|
|
19
|
+
| 表格 | 由 PDF 的框线还原网格、合并单元格、底色;单元格内文字按原字号缩放;跨页续表自动拼接并去掉重复表头 |
|
|
20
|
+
| 图片 | 优先取原始图像流(JPEG 原样、无损 RGB/灰度),取不到再按区域渲染;保留原始大小、左右缩进/居中对齐 |
|
|
21
|
+
| 并排插图 | 同一水平带的若干张图(如 a) b) c) d) 四宫格、两图并排)按原列位还原成无边框表格,不拆散、不重排 |
|
|
22
|
+
| 嵌入图 | 压在大图上的图例/局部放大图并入外层大图一起渲染,避免被当成「第二栏」错位 |
|
|
23
|
+
| 矢量图 | 曲线/色块聚类识别出的图表整体渲染成图片,并剔除其内部文字,避免文字重复 |
|
|
24
|
+
| 页眉页脚 | 跨页重复的行识别为页眉/页脚,页码写成 `PAGE`/`NUMPAGES` 域;可选择额外导出一份「无页眉页脚」版本 |
|
|
25
|
+
| 页面 | 按原页面尺寸和内容边界计算页边距,混合横竖版面时自动分节 |
|
|
26
|
+
| 鲁棒性 | 加密/损坏 PDF 用 `pypdf` 重写后再试;单页解析失败整页转图片兜底;过滤非法 XML 字符 |
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## 2. 环境与依赖
|
|
31
|
+
|
|
32
|
+
- Python **3.9+**(开发验证环境:Python 3.11.9 / Windows 10)
|
|
33
|
+
- 依赖(见 `requirements.txt`):
|
|
34
|
+
|
|
35
|
+
| 包 | 用途 | 验证版本 |
|
|
36
|
+
| --- | --- | --- |
|
|
37
|
+
| `pdfplumber` | 读取 PDF 页面、字符、表格、线条、图片 | 0.11.4 |
|
|
38
|
+
| `python-docx` | 生成 `.docx` | 1.2.0 |
|
|
39
|
+
| `pillow` | 图片解码/编码(PNG 输出) | 13.0.0 |
|
|
40
|
+
| `pypdf` | 修复损坏/加密 PDF 的兜底重写 | 7.1.0 |
|
|
41
|
+
|
|
42
|
+
`concurrent.futures`、`argparse`、`json`、`logging` 等均为标准库,无需额外安装。
|
|
43
|
+
|
|
44
|
+
### 安装
|
|
45
|
+
|
|
46
|
+
```shell
|
|
47
|
+
pip install pdf2word-pro
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
从源码安装(开发用):
|
|
51
|
+
|
|
52
|
+
```shell
|
|
53
|
+
pip install -r requirements.txt
|
|
54
|
+
pip install -e .
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## 3. 快速开始
|
|
60
|
+
|
|
61
|
+
```shell
|
|
62
|
+
pdf2word-pro input.pdf
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
不指定 `-o` 时,输出文件为「与 PDF 同名、同目录的 `.docx`」,并把最终路径打印到标准输出。
|
|
66
|
+
|
|
67
|
+
```shell
|
|
68
|
+
pdf2word-pro input.pdf -o output.docx
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## 4. 命令行参数
|
|
74
|
+
|
|
75
|
+
```text
|
|
76
|
+
usage: pdf2word-pro [-h] [-o OUTPUT] [--pages PAGES] [--password PASSWORD]
|
|
77
|
+
[--keep-page-breaks] [--no-columns] [--no-figures]
|
|
78
|
+
[--no-headings] [--no-header-footer]
|
|
79
|
+
[--remove-page-head-foot] [--no-fonts] [--dpi DPI]
|
|
80
|
+
[--cache-page] [--cache-dir CACHE_DIR] [--jobs JOBS] [-v]
|
|
81
|
+
pdf
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
| 参数 | 默认值 | 说明 |
|
|
85
|
+
| --- | --- | --- |
|
|
86
|
+
| `pdf` | 必填 | 输入 PDF 路径(位置参数) |
|
|
87
|
+
| `-o`, `--output` | 与输入同名 `.docx` | 输出 `.docx` 路径;目录不存在会自动创建 |
|
|
88
|
+
| `--pages` | 全部页 | 只转换指定页,1 开始计数。支持 `1-3,5,8-` 形式(`8-` 表示第 8 页到末页),自动裁剪越界值 |
|
|
89
|
+
| `--password` | 无 | 加密 PDF 的打开密码 |
|
|
90
|
+
| `--keep-page-breaks` | 关 | 在原 PDF 的分页处强制插入分页符(默认按内容自然流动重排) |
|
|
91
|
+
| `--no-columns` | 关 | 关闭双栏识别,按单栏整行读取 |
|
|
92
|
+
| `--no-figures` | 关 | 不把矢量图整体渲染成图片(只输出文字和位图) |
|
|
93
|
+
| `--no-headings` | 关 | 不套用 Word 标题样式,全部按正文段落输出 |
|
|
94
|
+
| `--no-header-footer` | 关 | 不单独识别页眉页脚(页眉页脚文字留在正文里) |
|
|
95
|
+
| `--remove-page-head-foot` | 关 | 除完整版外,**额外**保存一份去掉页眉/页脚/页码的 `<名>-no-head-foot.docx`,并把逐页检测结果写入控制台和 `<名>-page-head-foot.json` |
|
|
96
|
+
| `--no-fonts` | 关 | 不保留原字体名(统一用文档默认字体) |
|
|
97
|
+
| `--dpi` | `200` | 区域渲染图片(含兜底整页渲染)时使用的 DPI,越大越清晰、文件越大 |
|
|
98
|
+
| `--cache-page` | 关 | 把每一页的解析结果(JSON + 纯文本 + 图片)落盘,便于排查版面问题 |
|
|
99
|
+
| `--cache-dir` | 输出目录下 `_pdf2word_pages` | 逐页缓存目录,不存在会自动创建 |
|
|
100
|
+
| `--jobs` | `1` | 解析进程数;`>1` 且页数大于 8 时按页多进程并行(页面解析互不依赖,长文档可快数倍) |
|
|
101
|
+
| `-v`, `--verbose` | 关 | 输出 DEBUG 级日志,并在转换失败时抛出完整异常栈 |
|
|
102
|
+
|
|
103
|
+
退出码:`0` 成功;`1` 转换失败(日志里会打印原因,`-v` 可看完整栈)。
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## 5. 输出文件
|
|
108
|
+
|
|
109
|
+
以 `-o output.docx` 为例:
|
|
110
|
+
|
|
111
|
+
| 文件 | 何时产生 | 说明 |
|
|
112
|
+
| --- | --- | --- |
|
|
113
|
+
| `output.docx` | 总是 | 完整版,含页眉页脚 |
|
|
114
|
+
| `output-no-head-foot.docx` | 加 `--remove-page-head-foot` 时 | 去掉页眉/页脚/页码的版本 |
|
|
115
|
+
| `output-page-head-foot.json` | 加 `--remove-page-head-foot` 时 | 逐页检测到的页眉/页脚/页码内容 |
|
|
116
|
+
| `_pdf2word_pages\page_0001.json` 等 | 加 `--cache-page` 时 | 该页结构化解析结果(行/字符/表格/图片框,UTF-8 可读) |
|
|
117
|
+
| `_pdf2word_pages\page_0001.txt` | 加 `--cache-page` 时 | 该页纯文本视图(逐行文本 + 表格单元格文本) |
|
|
118
|
+
| `_pdf2word_pages\page_0001_*.jpg/png` | 加 `--cache-page` 时 | 该页提取出的图片/矢量图渲染结果 |
|
|
119
|
+
| `_pdf2word_pages\manifest.json` | 加 `--cache-page` 时 | 全部页的摘要索引(页号、块数、文件、耗时) |
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
## 6. 运行示例
|
|
124
|
+
|
|
125
|
+
### 6.1 整篇转换
|
|
126
|
+
|
|
127
|
+
```shell
|
|
128
|
+
pdf2word-pro input.pdf -o output.docx
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### 6.2 只检查某几页(排版回归时最常用)
|
|
132
|
+
|
|
133
|
+
```shell
|
|
134
|
+
pdf2word-pro input.pdf -o sample.docx --pages 12-14,20
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### 6.3 长文档多进程加速
|
|
138
|
+
|
|
139
|
+
```shell
|
|
140
|
+
pdf2word-pro input.pdf -o output-job8.docx --jobs 8
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### 6.4 去掉页眉页脚(同时保留完整版)
|
|
144
|
+
|
|
145
|
+
```shell
|
|
146
|
+
pdf2word-pro input.pdf -o output.docx --remove-page-head-foot
|
|
147
|
+
# 产出:output.docx、output-no-head-foot.docx、output-page-head-foot.json
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### 6.5 加密 PDF
|
|
151
|
+
|
|
152
|
+
```shell
|
|
153
|
+
pdf2word-pro encrypted.pdf -o decrypted.docx --password "你的密码"
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### 6.6 保留原始分页 + 关闭双栏识别(用于核对版面)
|
|
157
|
+
|
|
158
|
+
```shell
|
|
159
|
+
pdf2word-pro input.pdf -o output-layout.docx `
|
|
160
|
+
--keep-page-breaks --no-columns --cache-page --cache-dir _dbg -v
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### 6.7 作为库调用
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
from pdf2word_pro import Config, pdf_to_docx
|
|
167
|
+
|
|
168
|
+
cfg = Config(pages={45, 46, 47}, image_dpi=300, jobs=4) # pages 为 0 基页码
|
|
169
|
+
out = pdf_to_docx("input.pdf", "output.docx", cfg)
|
|
170
|
+
print(out)
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
`Config` 的字段与命令行参数一一对应(见 `Config` 定义处的注释),例如 `keep_page_breaks`、`detect_columns`、`render_figures`、`image_dpi`、`jobs`、`cache_pages`、`cache_dir`、`remove_page_head_foot`。
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
## 7. 实现流程
|
|
178
|
+
|
|
179
|
+
1. **逐页解析**(`parse_page`,pdfplumber)
|
|
180
|
+
- 表格:`find_tables()` → 网格 + 合并单元格 + 底色 + 单元格内文字/图片
|
|
181
|
+
- 图片:优先原始图像流,失败则按区域渲染;区域内嵌图(图例/局部图)并入外层图
|
|
182
|
+
- 矢量图:曲线/色块聚类后整体渲染,并剔除其内部文字
|
|
183
|
+
- 文字:字符 → 行 → 段落(字体/字号/粗斜体/颜色);自动识别双栏
|
|
184
|
+
2. **全局处理**
|
|
185
|
+
- 页眉页脚:跨页重复行识别,写入 Word 页眉/页脚,页码用域代码
|
|
186
|
+
- 段落折行合并:英文去连字符、中文不加空格
|
|
187
|
+
- 跨页合并:表格(列边界对齐 + 去重复表头)与段落(末行写满且下页无缩进)
|
|
188
|
+
3. **写 docx**(`write_docx`)
|
|
189
|
+
- 页面尺寸/页边距、对齐、缩进、行距/段间距、表格列宽行高、图片大小/对齐
|
|
190
|
+
- 并排图片写成无边框表格,一行图片不跨页、末行与图注同页
|
|
191
|
+
4. **鲁棒性**:加密/损坏 PDF 兜底、单页失败整页转图、非法 XML 字符过滤
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
## 8. 并排图片的排版策略
|
|
196
|
+
|
|
197
|
+
这是本工具最容易出问题的部分,实现要点:
|
|
198
|
+
|
|
199
|
+
- **成行判定**:同一水平带内的图片才归为一行;列数不一致的相邻行不合并,避免张冠李戴。
|
|
200
|
+
- **一图一列**:网格里每张图各占一列,相邻图以两张图边界的**中线**为界;不做单元格横向合并,因此即使原 PDF 里两张图轻微重叠,也不会出现「两张图挤进同一个合并单元格」的糊图。
|
|
201
|
+
- **行高与分页**:表格行设为 `cantSplit`(行内不跨页拆开),末行段落设 `keep_with_next`,使图片行与紧随其后的图注(`a) … b) …` 或 `图 x.x-x`)尽量同页。
|
|
202
|
+
- **内嵌图**:用包含关系(面积比 < 90% 且被完全包住)识别「图例/局部放大图」,把它并进外层大图的区域渲染结果里,而不是当成并排的第二张图。
|
|
203
|
+
- **对齐**:整行居中时按原表格左边界设置表格缩进,缩放比例按可用宽度等比计算,不拉伸变形。
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
## 9. 已知限制与排错
|
|
208
|
+
|
|
209
|
+
- **扫描版 PDF**:没有文字层,只能整页转图片(日志提示「未提取到任何内容 (可能是扫描件,需要先 OCR)」)。请先 OCR 再转换。
|
|
210
|
+
- **字体缺少 ToUnicode**:会出现「检测到 N 个无法映射到 Unicode 的字形」告警,这些字符只能丢弃/替换。
|
|
211
|
+
- **表格单元格内多张图并排**:目前按上下顺序输出,尚未做单元格内的并排还原。
|
|
212
|
+
- **矢量图形与位图叠加的图**:若底层是位图、彩色区域是矢量绘制,取原始图像流时可能只剩底图;此时可试 `--no-figures` 对比,或用 `--cache-page` 查看该页解析结果。
|
|
213
|
+
- **转换很慢**:加 `--jobs 8`;若怀疑某页卡住,用 `--pages` 单页定位并加 `-v` 看阶段耗时。
|
|
214
|
+
- **图片过大/过小**:调 `--dpi`(默认 200)。DPI 越高越清晰,`.docx` 也越大。
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
## 10. 目录结构
|
|
219
|
+
|
|
220
|
+
```text
|
|
221
|
+
pyproject.toml 打包配置(PEP 621 / setuptools)
|
|
222
|
+
LICENSE MIT 许可证
|
|
223
|
+
requirements.txt 依赖清单
|
|
224
|
+
README.md 本文件
|
|
225
|
+
README_EN.md 英文说明
|
|
226
|
+
pdf2docx.py 兼容入口(旧命令)
|
|
227
|
+
src/pdf2word_pro/ 包源码
|
|
228
|
+
core.py 转换实现(原 pdf2docx.py)
|
|
229
|
+
cli.py 命令行入口
|
|
230
|
+
__main__.py python -m 入口
|
|
231
|
+
__init__.py 对外接口
|
|
232
|
+
```
|