Python Forum
inserting photos in 1 sheet of a 6 sheet excel file fails
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
inserting photos in 1 sheet of a 6 sheet excel file fails
#1
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New on python. Needs help with Google sheet jeromep 1 151 Apr-25-2024, 06:47 PM
Last Post: deanhystad
  Plotting streamlines of the velocity field within an ice sheet verhaegenyoni 1 575 Mar-23-2024, 10:09 PM
Last Post: verhaegenyoni
  Python openyxl not updating Excel file MrBean12 1 344 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 455 Feb-07-2024, 12:24 PM
Last Post: Viento
  openpyxl issue - How to reset sheet.ins_row to the real last row ... ejwjohn 6 796 Nov-08-2023, 01:19 PM
Last Post: ejwjohn
  Search Excel File with a list of values huzzug 4 1,265 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 856 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  How to copy work sheet data one workbook to other? sayyedkamran 2 711 Nov-03-2023, 09:10 AM
Last Post: Larz60+
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 1,918 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,116 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020