pdf2word-pro
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.
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.
1. Features
| Area | What it does |
|---|---|
| Text | Rebuilds text as character → line → paragraph, keeping font, size, bold/italic and colour; no extra spaces for CJK, de-hyphenation for English |
| 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 |
| Headings | Short paragraphs noticeably larger than body text are mapped to Word Heading 1/2/3 styles (easy to restyle afterwards) |
| Columns | Detects two-column layouts by locating the gutter and emits content column by column |
| 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 |
| Images | Prefers the original image stream (JPEG as-is, lossless RGB/grey); falls back to region rendering; keeps original size, indentation and centring |
| 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 |
| 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 |
| 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 |
| 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 |
| Pages | Page margins are derived from the original page size and content extents; mixed portrait/landscape pages produce separate sections |
| 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 |
2. Requirements
- Python 3.9+ (developed and verified on Python 3.11.9 / Windows 10)
- Dependencies (see
requirements.txt):
| Package | Purpose | Verified version |
|---|---|---|
pdfplumber |
Read PDF pages, characters, tables, rules and images | 0.11.4 |
python-docx |
Build the .docx file |
1.2.0 |
pillow |
Image decoding / PNG encoding | 13.0.0 |
pypdf |
Fallback rewrite of damaged or encrypted PDFs | 7.1.0 |
concurrent.futures, argparse, json, logging, etc. are part of the standard library and need no extra installation.
Install
pip install pdf2word-pro
From source (development):
pip install -r requirements.txt
pip install -e .
3. Quick start
pdf2word-pro input.pdf
Without -o, the output is a .docx with the same name next to the input PDF, and its final path is printed to stdout.
pdf2word-pro input.pdf -o output.docx
4. Command-line options
usage: pdf2word-pro [-h] [-o OUTPUT] [--pages PAGES] [--password ***]
[--keep-page-breaks] [--no-columns] [--no-figures]
[--no-headings] [--no-header-footer]
[--remove-page-head-foot] [--no-fonts] [--dpi DPI]
[--cache-page] [--cache-dir CACHE_DIR] [--jobs JOBS] [-v]
pdf
| Option | Default | Description |
|---|---|---|
pdf |
required | Input PDF path (positional) |
-o, --output |
same name as input, .docx |
Output .docx path; missing directories are created |
--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 |
--password |
none | Password for an encrypted PDF |
--keep-page-breaks |
off | Insert a page break wherever the original PDF breaks a page (by default content reflows) |
--no-columns |
off | Disable two-column detection and read full-width lines |
--no-figures |
off | Do not render vector graphics as images (text and bitmaps only) |
--no-headings |
off | Do not apply Word heading styles; everything becomes body paragraphs |
--no-header-footer |
off | Do not extract headers/footers (their text stays in the body) |
--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 |
--no-fonts |
off | Do not keep original font names (use the document default) |
--dpi |
200 |
DPI used when rasterising regions (including the whole-page fallback); higher is sharper and larger |
--cache-page |
off | Dump each page's parse result (JSON + plain text + images) to disk for troubleshooting |
--cache-dir |
_pdf2word_pages next to the output |
Directory for the per-page cache; created if missing |
--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) |
-v, --verbose |
off | DEBUG-level logging, and re-raise the full traceback on failure |
Exit codes: 0 success, 1 conversion failed (the reason is logged; -v shows the full stack).
5. Output files
Assuming -o output.docx:
| File | Produced when | Description |
|---|---|---|
output.docx |
always | Full document, with headers/footers |
output-no-head-foot.docx |
with --remove-page-head-foot |
Same document with headers/footers/page numbers removed |
output-page-head-foot.json |
with --remove-page-head-foot |
Per-page header/footer/page-number detection result |
_pdf2word_pages\page_0001.json … |
with --cache-page |
Structured parse result of that page (lines/characters/tables/image boxes, UTF-8) |
_pdf2word_pages\page_0001.txt |
with --cache-page |
Plain-text view of that page (line text + table cell text) |
_pdf2word_pages\page_0001_*.jpg/png |
with --cache-page |
Images / rendered vector figures extracted from that page |
_pdf2word_pages\manifest.json |
with --cache-page |
Summary index of all pages (page number, block count, files, timing) |
6. Examples
6.1 Convert the whole document
pdf2word-pro input.pdf -o output.docx
6.2 Convert only a few pages (most common for layout regression)
pdf2word-pro input.pdf -o sample.docx --pages 12-14,20
6.3 Speed up a long document with multiple processes
pdf2word-pro input.pdf -o output-job8.docx --jobs 8
6.4 Remove headers and footers (keeping the full version)
pdf2word-pro input.pdf -o output.docx --remove-page-head-foot
# produces: output.docx, output-no-head-foot.docx, output-page-head-foot.json
6.5 Encrypted PDF
pdf2word-pro encrypted.pdf -o decrypted.docx --password "***"
6.6 Keep original page breaks and disable column detection (layout inspection)
pdf2word-pro input.pdf -o output-layout.docx `
--keep-page-breaks --no-columns --cache-page --cache-dir _dbg -v
6.7 Use it as a library
from pdf2word_pro import Config, pdf_to_docx
cfg = Config(pages={45, 46, 47}, image_dpi=300, jobs=4) # pages are 0-based
out = pdf_to_docx("input.pdf", "output.docx", cfg)
print(out)
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.
7. How it works
- Per-page parsing (
parse_page, pdfplumber)- Tables:
find_tables()→ grid + merged cells + shading + text/images inside cells - Images: original image stream first, region rendering as a fallback; inset images (legends/details) are merged into the outer figure
- Vector graphics: curves and filled shapes are clustered, rendered as one image, and their internal text is removed
- Text: characters → lines → paragraphs (font/size/bold/italic/colour); two-column detection
- Tables:
- Global passes
- Headers/footers: repeating cross-page lines are recognised and written as Word headers/footers, page numbers as fields
- Line-join: English de-hyphenation, no spaces for CJK
- Cross-page merging: tables (column-aligned, duplicate header rows removed) and paragraphs (last line full and next page not indented)
- Writing the docx (
write_docx)- Page size/margins, alignment, indentation, line/paragraph spacing, table column widths and row heights, image size/alignment
- Side-by-side figures become a borderless table; a figure row is never split across pages and its last row stays with the caption
- Robustness: encrypted/damaged PDFs, whole-page fallback on parse failure, illegal XML character filtering
8. How side-by-side figures are laid out
This is the most error-prone part of any PDF-to-Word conversion. Key decisions:
- 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.
- 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.
- Row height and pagination: table rows are set to
cantSplit(a row never breaks across pages) and the last row's paragraphs usekeep_with_next, so a figure row stays on the same page as the caption that follows it (a) … b) …orFigure x.x-x). - 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.
- 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.
9. Known limitations and troubleshooting
- 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.
- Fonts missing ToUnicode: you will see a "N glyphs could not be mapped to Unicode" warning; those characters can only be dropped or replaced.
- Multiple images side by side inside a table cell: currently emitted in vertical order; per-cell side-by-side layout is not implemented yet.
- 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. - Slow conversion: add
--jobs 8; to find a stuck page, isolate it with--pagesand add-vto see stage timings. - Images too large / too small: adjust
--dpi(default 200). Higher DPI is sharper and produces a larger.docx.
10. Repository layout
pyproject.toml packaging config (PEP 621 / setuptools)
LICENSE MIT license
requirements.txt dependency list
README.md Chinese documentation
README_EN.md this file
pdf2docx.py legacy entry point
src/pdf2word_pro/ package source
core.py converter implementation
cli.py command-line entry
__main__.py python -m entry
__init__.py public API
Metadata
Release files for pdf2word-pro 0.1.0
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 |
|---|---|---|---|---|
| pdf2word_pro-0.1.0.tar.gz | 52.5 kB | Without provenance | Details for pdf2word_pro-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pdf2word_pro-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pdf2word_pro-0.1.0-py3-none-any.whl |
Total release size: 95.2 kB
Release files / pdf2word_pro-0.1.0.tar.gz
| Download URL | pdf2word_pro-0.1.0.tar.gz |
|---|---|
| Size | 52.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3203591856ba4d24c6060fb52a04ed57f93a8b627ef6a0686d5fc2e98b870534
|
|
BLAKE2b-256 checksum How to use checksums |
c33095fe7c5556f0ad48607c4f3d9219368a317bff9f08a5d521501112e8d7e2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.9
|
Release files / pdf2word_pro-0.1.0-py3-none-any.whl
| Download URL | pdf2word_pro-0.1.0-py3-none-any.whl |
|---|---|
| Size | 42.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6489aaab33905d80dd486f8bcf9fc7562cd2f4f90c1f85d6339003d94debfc36
|
|
BLAKE2b-256 checksum How to use checksums |
f89719f4041186da23b44cdf90ed9856858ff67083fa8f66e48b8e3b7067ddc2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.9
|
Security for pdf2word-pro 0.1.0
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A 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.