1.统计文件夹结构下信息
import os import openpyxl from openpyxl import Workbook # 询问用户输入文件夹路径 folder_path = input("请输入文件夹路径: ") # 定义图片文件扩展名 image_extensions = ['.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp'] def is_image_file(filename): return any(filename.lower().endswith(ext) for ext in image_extensions) def cleanup_folder(folder): for root, dirs, files in os.walk(folder): for file in files: if not is_image_file(file) and not os.path.isdir(os.path.join(root, file)): os.remove(os.path.join(root, file)) def find_image_folders(folder): image_folders = [] for root, dirs, files in os.walk(folder): if any(is_image_file(file) for file in files): image_folders.append(root) return image_folders def create_excel(folder, image_folders): # 获取文件夹名称作为表格名称 folder_name = os.path.basename(os.path.normpath(folder)) excel_path = os.path.join(folder, f"{folder_name}.xlsx") # 创建Excel工作簿 wb = Workbook() ws = wb.active ws.title = "Image Folders" # 将文件夹名称写入第一列和第二列 for idx, image_folder in enumerate(image_folders, start=1): folder_name_only = os.path.basename(image_folder) ws.cell(row=idx, column=1, value=image_folder) ws.cell(row=idx, column=2, value=folder_name_only) # 保存Excel文件 wb.save(excel_path) def main(): # 清理文件夹,删除非图片和非文件夹类文件 cleanup_folder(folder_path) # 查找所有包含图片的文件夹 image_folders = find_image_folders(folder_path) # 创建Excel表格并写入数据 create_excel(folder_path, image_folders) if __name__ == "__main__": main()
2.excel表格内部处理,只留中文(ctrl+shirft+enter)(b2)
=TEXTJOIN("", TRUE, IF(UNICODE(MID(B2, ROW(INDIRECT("1:" & LEN(B2))), 1)) >= 19968, MID(B2, ROW(INDIRECT("1:" & LEN(B2))), 1), ""))
3.复制数据到新表格(只有值),并操作以下代码。
import os import shutil import pandas as pd def get_user_input(): excel_path = input("请输入Excel表格路径: ") folder_path = input("请输入文件夹地址: ") input_col_index = int(input("请输入输入列号(从1开始): ")) - 1 rename_col_index = int(input("请输入重命名列号(从1开始): ")) - 1 return excel_path, folder_path, input_col_index, rename_col_index def read_excel(excel_path, input_col_index, rename_col_index): try: df = pd.read_excel(excel_path) if df.empty: print("Excel文件为空,请检查文件内容。") return None, None print("读取到的Excel表格内容:") print(df.head()) num_columns = len(df.columns) if input_col_index >= num_columns or rename_col_index >= num_columns: raise IndexError(f"列号超出范围,请检查输入的列号是否正确。表格有 {num_columns} 列。") input_col = df.iloc[:, input_col_index].astype(str).str.strip() rename_col = df.iloc[:, rename_col_index].astype(str).str.strip() return input_col, rename_col except FileNotFoundError: print(f"文件未找到: {excel_path}") return None, None except ValueError as e: print(f"读取Excel文件时发生值错误: {e}") return None, None except Exception as e: print(f"读取Excel表格时发生错误: {e}") return None, None def delete_folder(path): try: shutil.rmtree(path) print(f"成功删除文件夹: {path}") except Exception as e: print(f"删除文件夹 {path} 时发生错误: {e}") def process_folders(folder_path, input_col, rename_col): if not os.path.exists(folder_path): print(f"文件夹路径不存在: {folder_path}") return for root, dirs, _ in os.walk(folder_path, topdown=False): if not dirs: folder_name = os.path.basename(root) if folder_name in input_col.values: new_name = rename_col[input_col[input_col == folder_name].index[0]] new_folder_path = os.path.join(os.path.dirname(root), new_name) if new_name not in os.listdir(os.path.dirname(root)): try: os.rename(root, new_folder_path) print(f"重命名文件夹 {root} 为 {new_folder_path}") except Exception as e: print(f"重命名文件夹 {root} 时发生错误: {e}") else: print(f"文件夹 {new_folder_path} 已存在。删除新的文件夹 {root}.") delete_folder(root) else: print(f"文件夹 {folder_name} 在输入列中未找到。删除文件夹 {root}.") delete_folder(root) def main(): excel_path, folder_path, input_col_index, rename_col_index = get_user_input() input_col, rename_col = read_excel(excel_path, input_col_index, rename_col_index) if input_col is not None and rename_col is not None: process_folders(folder_path, input_col, rename_col) else: print("处理停止。请检查输入并重试。") if __name__ == "__main__": main()
标签:批量,修改,文件夹,input,path,folder,os,col From: https://www.cnblogs.com/zly324/p/18242161