I'm automating an Excel report batch-creation creation process. Excel files are used as report templates, and placeholders like ^SOME_DATA^
are replaced via a Python script which unzips the .XLSX file, performs a basic find and replace inside sharedStrings.xml
and sheet1.xml
files, and then re-zips finished file. This part works (finally).
When opening the new file in Excel, however, the numbers that replaced the placeholders all are flagged as "Number Stored As Text" by Excel (which causes both formulas and formatting in the worksheet to fail, of course). Most of the placeholders were stored by Excel in sharedStrings.xml
, and given the "strings" part of that I'm not surprised they're still treated as strings.
So far...
I tried casting with
=VALUE(A1)
(etc.) to convert the text-numbers to actual numbers... this works interactively, but fails when the file is created programmatically: The formula cell shows as#VALUE!
(Popup: "Error in Value") unless you edit it and hit enter.I prefer Python as it fits with the rest of the automation, but could redo the find/replace/save code in VBA to "get it out the door" in a less-automated state. I've not had much luck with VBA solutions being reliable/robust, personally, however.
openpyxl
works (reasonably) well for reading OR writing Excel, but doesn't appear to support making edits to an existing formatted file.