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.

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.

import os
import shutil
import openpyxl

# Variables
errorlog = []
gtins_from_excel = set()
gtins_from_pictures = set()
source_folder = "/Users/user1/Downloads/latest"
destination_folder = "/Users/user1/Downloads/requests"
excel_path = "/Users/user1/Downloads/Mappe1.xlsx"

# Extracting GTINs from filenames in the source folder
for filename in os.listdir(source_folder):
    if '.' in filename:  # Ensure that the filename has an extension
        gtins_from_pictures.add(filename.split(".")[0])

# Extracting GTINs from the Excel file
wb_obj = openpyxl.load_workbook(excel_path)
sheet_obj = wb_obj.active

for i in range(1, sheet_obj.max_row + 1):
    cell_obj = sheet_obj.cell(row=i, column=1)
    gtins_from_excel.add(str(cell_obj.value))  # Convert to string just in case

# Check for matches and copy files
for gtin in gtins_from_excel:
    if gtin in gtins_from_pictures:
        source_file = os.path.join(source_folder, f"{gtin}.jpg")  # Assuming .jpg extension
        dest_file = os.path.join(destination_folder, f"{gtin}.jpg")
        shutil.copy2(source_file, dest_file)
    else:
        errorlog.append(gtin)

print("Errorlog:", errorlog)

Changes I made:

  1. Used sets for gtins_from_excel and gtins_from_pictures for faster membership checks.
  2. Simplified the process to copy files using the GTIN directly.

Leave a Comment