Excel said the file had errors. The file was fine. Twice.

Title card: Excel said the file had errors. The file was fine. Twice.

“We found a problem with some content. Do you want us to try to recover as much as we can?”

That’s the dialog a travel advisor gets when they open a workbook our tool generated. The data is all there once they click Yes. It also looks exactly like a broken product, and I’d rather it didn’t.

What made this one awkward is that I couldn’t see it. My copy of Excel opened every file cleanly. LibreOffice opened them cleanly. The prompt only showed up for some people, on some trips, and only in real Excel, which is a stricter reader than anything else I’d tested against.

It turned out to be two separate bugs with the same symptom.

Bug one: a date with two timezones

A workbook is a zip of XML files. One of them, docProps/core.xml, holds the created and modified times. The format for those is strict. A valid value looks like this:

2026-11-13T03:54:35Z

The files coming out of the build had this:

2026-11-13T03:54:35+00:00Z

An offset and a Z. Two ways of saying UTC, glued together, and Excel declines to read it as a date at all.

The cause was the library version. The styling step runs a Python library over the generated workbook, and the server had the version that Ubuntu 24.04 ships. At save time that version stamps the modified time itself, as a timezone-aware value, and its writer appends the Z regardless. Newer releases of the library fixed it. My laptop had a newer one, which is why I never saw it.

I couldn’t set the property beforehand either, because the save overwrites it. So the fix runs after the save and rewrites that one file inside the zip:

import re, zipfile, os

def repair_document_dates(path):
    pattern = re.compile(r"(\d{2}:\d{2}:\d{2})(?:\.\d+)?[+-]\d{2}:\d{2}Z")
    tmp = path + ".tmp"
    with zipfile.ZipFile(path) as zin, zipfile.ZipFile(tmp, "w", zipfile.ZIP_DEFLATED) as zout:
        for item in zin.infolist():
            data = zin.read(item.filename)
            if item.filename == "docProps/core.xml":
                data = pattern.sub(r"\1Z", data.decode("utf-8")).encode("utf-8")
            zout.writestr(item, data)
    os.replace(tmp, path)

It’s ugly. It also does nothing on a newer library, which is the behaviour I want, and the test feeds it the exact bad string so I’m not relying on the server happening to be old.

Bug two: the ampersand

This one was why it only hit some trips. Every sheet has a print footer with the trip name on the left. In a header or footer, Excel treats & as the start of a code: &P is the page number, &A the sheet name, and so on.

So a trip called “Nassau & Harbour Island” wrote a footer that begins Nassau & Harbour..., and H isn’t a code. Invalid footer, repair prompt. A trip called “Greek Islands” was perfect. I’d been testing with trips like that.

The fix is to double it, which is how a literal ampersand is written there:

import re
footer = re.sub(r"\s+", " ", str(trip_name)).replace("&", "&&")[:200]

I tidied whitespace and capped the length while I was in there, since the name goes straight into an XML attribute and I didn’t love how much I trusted it.

Why I didn’t catch either

Both defects are invisible to anything lenient. LibreOffice shrugs and reads on. A PHP test that opens the workbook and checks the cells would pass. The only judge that notices is the one your customers are using, and it only speaks up when a particular combination of library version and punctuation turns up.

So the tests now check the XML directly: the dates must match the plain UTC pattern, and every footer, once the doubled ampersands are taken out, must contain no & that isn’t a real code. A bad input in the test is worth more than another happy-path one.

If your generated files “work” for you and not for them, unzip one and read the XML. It’s a good deal more informative than the dialog.

Leave a Reply

Your email address will not be published. Required fields are marked *