appending cell objects to write_only worksheet corrupts saved .xlsx files
When copying cell objects from one worksheet into another, worksheet.append() will normally raise ValueError: Cells cannot be copied from other worksheets
. This error is not raised if the appended worksheet is in write_only mode, which corrupts the resulting file when saved.
Code snippet to reproduce:
import openpyxl as oxl
def parse_n_save_snippet():
wb = oxl.load_workbook(filename='snippet_example.xlsx')
wb2 = oxl.Workbook(write_only=True) # Swap to False to get proper ValueError
for sheet in wb.worksheets:
ws2 = wb2.create_sheet(sheet.title)
if(wb2.active is None):
wb2.active(sheet.title)
for row in sheet.iter_rows(values_only=False):
ws2.append(row)
wb2.save("test2.xlsx") # Corrupted file
Example .xlsx I'm using is here:
snippet_example.xlsx
openpyxl version: 3.0.4
Python version: 3.7
Edited by AuroranAI