pythonopenpyxlshutilpull

Pulling pictures from folder based on excel list


I am currently just playing around and getting to know python as a whole. My current "Task" is to pull pictures from an already existing folder on my HDD based on a list of picture names given in an excel list.

Weirdly however the code only works with a specific excel list when it should be working with whatever list it gets.

Further description and code example down below. Thank you in advance :)

import os
import shutil
#Variablen:
errorlog = []
gtins_from_excel = []
gtins_from_pictures = []
list = []
dir = os.listdir("/Users/user1/Downloads/latest") #import "pictures" to python
print(len(dir))
for i in dir:
    try:
        gtins_from_pictures.append((i.split(".")[0])) #converting picture names to same format       #                                                      as excel list 
    except:
        pass
# importing openpyxl module
import openpyxl
# Give the location of the file
path = "/Users/user1/Downloads/Mappe1.xlsx"
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active
m_row = sheet_obj.max_row
# Loop will print all values
# of first column
for i in range(1, m_row + 1):
    cell_obj = sheet_obj.cell(row=i, column=1)
    gtins_from_excel.append(cell_obj.value)
#print((gtins_from_excel))
#print(len(gtins_from_pictures))
#print(len(dir))
errorlog = []
for i in range(0, len(gtins_from_excel)):
    flag = 0
    for j in range(0, len(gtins_from_pictures)):
        print(gtins_from_excel[i], "<>", gtins_from_pictures[j])
        try:
            if gtins_from_excel[i] == gtins_from_pictures[j]:
                flag = 1
                copy = shutil.copy2("/Users/user1/Downloads/latest/" + dir[j], "/Users/user1/Downloads/requests/" + dir[j])
        except:
            pass
    #print(flag, " ", gtins_from_excel[i])
    if flag == 0:
        errorlog.append(gtins_from_excel[i])
print("Errorlog:", errorlog)


Output for "Mappe1.xlsx" (consists of 2 Entries:4004764000364, 4004764000517 -> Both of which are found in the picture folder): . . . 4004764000517 <> 4004764000517 (Proof it is found in the folder) . 4004764000364 <> 4004764000364 (Same here) . Errorlog: [4004764000364, 4004764000517]

Yet the error log lists both of those numbers as "not found" and I don't know what the cause is.


Solution

  • if gtins_from_excel[i] == gtins_from_pictures[j]:
        ...
    

    I would start by making sure that gtins_from_excel[i] and gtins_from_pictures[j] are of the same data type. If one is a str and the other is an int, then that if condition would evaluate to False.