# SPDX-License-Identifier: AGPL-3.0-or-later """ PDF -> Word (.docx) for ToolDabba's "PDF to Word" tool (https://tooldabba.com/pdf-to-word/). Uses pdf2docx (MIT) and PyMuPDF (AGPL-3.0, Artifex). Because PyMuPDF is AGPL, this program is AGPL too and its complete source is published at https://tooldabba.com/source/pdf_to_docx.py. Neither library is modified. python pdf_to_docx.py input.pdf output.docx Prints one line of JSON on success, e.g. {"pages": 3, "scanned": false}. Exit codes: 3 = needs a password, 4 = too many pages, 5 = not a readable PDF, 1 = conversion failed. """ import json import sys import pymupdf from pdf2docx import Converter MAX_PAGES = 100 def done(code, **info): print(json.dumps(info)) sys.exit(code) def main(src, dst): try: doc = pymupdf.open(src) except Exception: done(5, error="invalid") if doc.needs_pass: done(3, error="password") pages = doc.page_count if pages > MAX_PAGES: done(4, error="too-many-pages") # A scan has (almost) no text layer: Word will get pictures of the pages, not editable text chars = sum(len(page.get_text("text").strip()) for page in doc) doc.close() try: converter = Converter(src) converter.convert(dst, multi_processing=False) converter.close() except Exception: done(1, error="failed") done(0, pages=pages, scanned=chars < 20 * pages) if __name__ == "__main__": main(sys.argv[1], sys.argv[2])