Corrupted File when saving to BufferedRandom (i.e., when using 'with open(file) as f:')
(This is my first reported issue, apologies for any mistakes!)
Description
When opening an existing workbook using 'with open' and saving it, Excel reports a corrupted file. (Additionally, the file gets bigger each time the script is run...)
Minimal Code-Snippet
# 'file.xlsx' is an empty Excel-File created by Microsoft Excel
from openpyxl import load_workbook
with open('file.xlsx', mode='rb+') as f:
wb = load_workbook(f)
# f.seek(0) --> This would create a corrupted (but repairable) file that can NOT be opened by openpyxl anymore
wb.save(f)
# When opening the file with Microsoft Excel again, it reports a corrupted (but repairable) file
System (checked with clean conda environment)
- Python 3.12.2
- Openpyxl 3.1.2
Why saving to BufferedRandom should be possible
When dealing with file-locks (e.g., through portalocker), a file-handle has to be used to access the file (instead of a file-name). Also see https://openpyxl.readthedocs.io/en/3.1/tutorial.html#saving-as-a-stream for a description on how to use it with NamedTemporaryFile().
Thanks
Thank you for your support! Great library - just got into saving multiple formats to a single cell, which also worked like a charm!