Python Forum

Full Version: openpyxl saving files issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is there anybody here who knows openpyxl well?

I wanted to make a little program to insert student photos next to their names. This is the first time I have used python. I managed to make it work and successfully insert the images in the correct cells.

I only opened 1 sheet of the file and made it the active sheet. At the end of the code is:

wb.save(pathToExcel + newname)

However, when I then save the file, the photos which are already in the other sheets are all gone! Not 1 photo left in any other sheet.

This is somewhat unexpected, as I did not touch those sheets.

Can anyone explain this or tell me how to prevent this?

Edit: I just opened an excel file, opened the first sheet, which is class 4, then saved it again. All the photos are now gone! Below is from the interactive shell.

Quote:>>> sheets = wb.get_sheet_names()
>>> sheets
['课程表2017-2018冬季', '商务英语2016(4)', '商务英语2016(1)', '商务英语2016(3)', '商务英语2016(2)']
>>> ws = wb.get_sheet_by_name(sheets[1])
>>> ws
<Worksheet "商务英语2016(4)">
>>> wb.save(pathToExcel + nameOfFile)
>>>
This is from the openpyxl docs:
Quote:Warning
openpyxl does currently not read all possible items in an Excel file so images and charts will be lost from existing files if they are opened and saved with the same name.

The images will also be lost if they are saved in a file with another name! That is what I find!