import os import openpyxl def list_images_to_excel(directory): # 创建一个新的Excel工作簿 workbook = openpyxl.Workbook() sheet = workbook.active # 遍历目录中的文件 row = 1 for filename in os.listdir(directory): # 检查文件是否是图片 if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff', '.webp')): # 写入文件名(不含扩展名) sheet.cell(row=row, column=1, value=os.path.splitext(filename)[0]) row += 1 # 保存Excel文件 workbook.save(os.path.join(directory, "image_list.xlsx")) def main(): # 询问用户输入目录路径 target_directory = input("请输入您要遍历的目录路径: ") list_images_to_excel(target_directory) if __name__ == "__main__": main()
标签:11,__,filename,directory,main,os,row From: https://www.cnblogs.com/zly324/p/17916693.html