Python Forum
inserting photos in 1 sheet of a 6 sheet excel file fails - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: inserting photos in 1 sheet of a 6 sheet excel file fails (/thread-8671.html)



inserting photos in 1 sheet of a 6 sheet excel file fails - Pedroski55 - Mar-03-2018

Hi, this is a weird one, but maybe someone has an inkling:

First of all: I am not good at Python, but have made it do what I want. openpyxl has a problem, in that, when it loads an excel file, it does not load the images, so they must be reinserted each time an excel file is manipulated, or they will not be in the saved workbook.

I keep all my class records in a spreadsheet, attendance, scores, comments. 1 sheet for each class.

When I get a new class, I take their photos and put them next to the names and numbers.

The photos are saved in folders with exactly the same names as the sheets for each class. Within the program I can reference the photos-folders from the sheet names.

Using openpyxl, the following code does this puts the photos in column 1:

sheets = wb.get_sheet_names()
for sheet in sheets:
    ws = wb.get_sheet_by_name(sheet)
    print('The active sheet is ' + sheet)
    for filename in os.listdir(pathToPhotoFolder + sheet):
        if not (filename.endswith('.jpg')):
                continue
        for rowNum in range(beginRow, ws.max_row + 1):
                getName = filename.split('.')
                if (getName[0] == ws.cell(row=rowNum, column=col).value):
                        print('found the name! Name is: %s ' % (getName[0]))
                        print('Putting pic in row ' + str(rowNum))
                        img = Image(pathToPhotoFolder + sheet + '/' + filename)
                        ws.add_image(img, 'A' + str(rowNum))
wb.save(pathToExcel + newname)
This works for 5 out of 6 sheets, but fails each time for class 17BE3.

When I run the program in a terminal, it tells me it has found and inserted the photos for 17BE3:

This is from the terminal output:

Quote:The active sheet is 17BE3
found the name! Name is: 李梦兰
Putting pic in row 28
found the name! Name is: 张焕铃
Putting pic in row 46
found the name! Name is: 董职鑫
Putting pic in row 31

But the photos for 17BE3 are not present in the saved file. All other photos, in all other sheets are present.

I copied just 17BE3 sheet to a separate excel file, and ran the program over it.

The photos were inserted correctly!!

I also tried moving the sheet within the excel file, but 17BE3 remains photoless, although all other sheets have the photos.

Does anyone have an inkling, vague idea, guess as to what is wrong?