0

I'm trying to save pandas DF into an existing spreadsheet. I found an excellent answer at Writing Pandas DataFrame to Excel: How to auto-adjust column widths, which is really continuation of another question *)

The problem though is that when I use it, on trying to load the spreadsheet I get an error on "damaged content", complaining about a drawing - even though I have none in the spreadsheet, and all functions are gone. Static data are still there. log is

<?xml version="1.0" encoding="UTF-8" standalone="true"?>

-<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">

<logFileName>error171360_05.xml</logFileName>

<summary>Errors were detected in file 'test.xlsx'</summary>


-<repairedRecords summary="Following is a list of repairs:">

<repairedRecord>Repaired Records: Drawing from /xl/drawings/drawing1.xml part (Drawing shape)</repairedRecord>

</repairedRecords>

</recoveryLog>

Any ideas?

Edit: I'm pretty sure now it's not caused by pandas, as opening workbook, adding an empty sheet, and saving it removes all the formulas.

workbook = load_workbook(file)
try:
    sheet = workbook["Result"]
except KeyError:
    sheet = workbook.create_sheet("Result")
# for r in dataframe_to_rows(result, index=False, header=True):
#     sheet.append(r)
workbook.save(file)

It doesn't produce the error above though.

Edit2: There's a question from 2013 (Openpyxl: Formulas getting removed when saving file) which says OpenPyxl doesn't support it, with a feature requested to do so. But the link to the feature doesn't work, so I have no idea whether it works or not.

*) there is a small bug in the function in that answer, sheet_name is a param, but it also tries to look it up in **kwargs, which of course fails, so gets replaced by a default value even if passed into the function. I can't comment on the question, so maybe @maxU will read this and edit..

vlade
  • 93
  • 1
  • 7
  • Try an older version of pandas, not sure which one, but if I remember correctly, this happens in the newer versions of pandas. – Erfan Jun 30 '21 at 19:46
  • Doh. Any idea which version of pandas might work? I'm on 1.2.4 – vlade Jun 30 '21 at 19:50
  • Not sure, try 1.1.5 – Erfan Jun 30 '21 at 19:50
  • ok, thanks. Worst comes to worst, I can just output it directly using openpyxl, if the problem is in pandas. – vlade Jun 30 '21 at 19:56
  • Still doesn't work, either with 1.1.5 or 1.3.0rc1. With some more testing, I'm pretty sure it's not pandas problem, because simple loading a workbook, adding a new (empty) sheet, and saving it causes the formulas to dissappear too. – vlade Jul 01 '21 at 05:56
  • The description is incorrect and the linked question is irrelevant. – Charlie Clark Jul 01 '21 at 07:31

0 Answers0