Python Forum

Full Version: Reading Excel files and creating an output
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi.

I haven't used Python in a few years and even when I did it was at a very basic level.

I have numerous excel files (e.g. file a.xlsb, b.xlsb) that have been populated and in column "M" there are photo reference numbers (e.g. 1, 2, 43, etc.). I have a folder with all the photos referenced in however there are almost 5000 of them and I need to arrange them into folders as per the excel file (e.g. C:\Pictures\a). The pictures are just numbered the same, 1.jpg, 2.jpg, etc.

The problem I have is that the numbers in the file are not sequential so arranging them manually will take a very long time. Is there a way that I can use python to read the numbers in column M, find the relevant photo in the pictures folder, and then copy those photos into a new folder that corresponds to the file being read?

I hope this makes sense.

Thanks in advance for any help.
Yes :)

Here's some pseudocode to help:
import shutil
from openpyxl import load_workbook

for file_prefix in ["a", "b"]:
    doc = load_workbook(filename= file_prefix + '.xlsx')
    sheet = doc['Sheet1']

    from_path = './'
    to_path = '/images/' + file_prefix + '/'

    for row in sheet.rows:
        image_id = row['M'] + ".jpg"
        shutil.copyfile(from_path + image_id, to_path + image_id)