根据excel表中的图片名称,到文件夹中找出与之对应的图片,并另存为到一个文件夹中。
import os
import pandas as pd
import shutil
excel_path = '你的Excel文件路径.xlsx'
source_folder = '你的图片源文件夹路径'
destination_folder = '你的目标文件夹路径'
df = pd.read_excel(excel_path)
image_names = df['图片名称列'].tolist()
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
for image_name in image_names:
image_path = os.path.join(source_folder, image_name)
if os.path.exists(image_path):
shutil.copy(image_path, destination_folder)
print(f'复制 {image_name} 到目标文件夹.')
else:
print(f'图片 {image_name} 不存在于源文件夹中.')
print('操作完成.')