Documents and spreadsheets
Generating .docx and .xlsx files that survive a real reader: tracked changes, page setup, merged cells, and why converted PDFs are not templates.
- LibreOffice converts a PDF form to .docx and python-docx sees no paragraphs at all
A headless LibreOffice PDF-to-docx conversion produces a file that looks perfect in a word processor but reports zero paragraphs and zero tables to python-docx. Why the import filter gives you shapes, and how rebuilding the form with pdftotext is the cheaper route.
- openpyxl: copy_worksheet drops the page setup and insert_rows leaves merged cells behind
copy_worksheet() silently resets print scaling and insert_rows() leaves merged ranges on their old coordinates. Both look correct in the cell grid and wrong on paper. How I caught them by rendering to PDF, and the code that fixes each.
- openpyxl: the second save() of a workbook with images raises I/O operation on closed file
Editing a spreadsheet that contains a logo, saving it twice from the same Workbook object, and getting ValueError: I/O operation on closed file — plus a truncated output file that no longer opens. Why images are the trigger and why one load per save is the fix.
- Word tracked changes in a .docx: python-docx cannot do it, raw XML can
python-docx has no API for revisions, so a document that must be reviewed with accept/reject has to be edited as OOXML. The w:ins and w:del structures, the unique-id rule that silently corrupts documents, and how to prove accept and reject both work before you send it.