Editing Office files without breaking them
Word, Excel and PowerPoint are ZIP archives of XML. Here's how to change the text and keep every font, table and chart intact.
A .docx is just a ZIP of XML. That sounds easy — until you replace some text and watch the formatting detonate. Translating Office documents on-device (Bobr Translate) taught me that the goal isn’t parsing the file, it’s touching as little of it as possible.
Runs are where formatting hides
Word splits a sentence into “runs” — spans that each carry their own styling. “Hello world” might be three runs because the spell-checker once nudged it. Translate run-by-run and you get fragmented nonsense; flatten everything into one run and you lose the bold. The move is to merge adjacent runs that share formatting, transform the merged text, then write it back into the first run and empty the rest.
Don’t touch what you don’t understand
Tables, charts, drawings, footnotes, hyperlinks — each lives in its own XML part with its own rules. The safe rule is brutal and simple: only rewrite the text nodes you can positively identify (w:t in Word, a:t in PowerPoint, the shared-string table in Excel) and leave every other byte exactly as you found it.
Excel keeps its text in one shared table
Excel stores strings once in sharedStrings.xml and references them by index from cells. Rewrite an entry and you change every cell that points to it — efficient, but watch for inline strings and never, ever touch formula cells.
Verify by reopening, not by diffing
The only test that matters: does Office open the result without the “we found a problem, want to repair?” dialog? XML diffs lie; the real apps are ground truth. Round-trip every sample through actual Word, Excel and PowerPoint before you trust the pipeline.
Preserve-everything beats parse-everything. Touch the text, fear the rest.