python批量导出excel表格并且重命名为其中某个相对应列的值
今天小伙伴找到我问我怎么把图片批量导出来并且给他重命名相对应的编码,于是我就想到用python来进行此类操作,不然一个个去重命名和导出,少点的还好,但是做电商的怎么可能只有一点点呢,所以就有了这个软件下面我放个演示,源码和打包后的软件也会放在下方,麻烦看下广告,纯粹为爱发电!谢谢!!
这种方式仅支持图片浮动方式,如果是嵌入的先把图片改为浮动再用
源码:
import tkinter as tk
from tkinter import filedialog, messagebox, simpledialog, scrolledtext
from openpyxl import load_workbook
from openpyxl_image_loader import SheetImageLoader
import os
def browse_excel_file():
file_path = filedialog.askopenfilename(
title="选择Excel文件",
filetypes=[("Excel files", "*.xlsx")]
)
if file_path:
excel_path.set(file_path)
log_message("Excel文件已选择: " + file_path)
def browse_save_dir():
directory = filedialog.askdirectory(title="选择保存路径")
if directory:
save_dir.set(directory)
log_message("保存路径已选择: " + directory)
def get_sheet_name():
sheet_name = simpledialog.askstring("输入工作表名称", "请输入工作表名称:", parent=root)
if sheet_name:
sheet_name.set(sheet_name)
log_message("工作表名称已设置为: " + sheet_name)
def process_excel():
try:
excel_path_val = excel_path.get()
save_dir_val = save_dir.get()
sheet_name_val = sheet_name.get()
image_column_val = image_column.get()
rename_column_val = rename_column.get()
if not all([excel_path_val, save_dir_val, sheet_name_val, image_column_val, rename_column_val]):
messagebox.showerror("Error", "请填写所有必须的字段")
return
wb = load_workbook(excel_path_val)
if sheet_name_val in wb.sheetnames:
ws = wb[sheet_name_val]
log_message("正在处理工作表: " + sheet_name_val)
image_loader = SheetImageLoader(ws)
num = ws.max_row
for i in range(2, num + 1):
name = ws[f"{rename_column_val}{i}"].value
image = image_loader.get(f"{image_column_val}{i}")
if image and name:
image_format = image.format
image.save(os.path.join(save_dir_val, f"{name}.{image_format.lower()}"))
log_message(f"图片已保存: {name}.{image_format.lower()}")
else:
log_message("某些行可能未包含图片或文件名")
else:
messagebox.showerror("Error", "工作表名称不正确")
log_message("错误: 工作表名称不正确")
except Exception as e:
messagebox.showerror("Error", f"处理Excel时发生错误: {e}")
log_message(f"错误: {e}")
def log_message(message):
log_text.config(state=tk.NORMAL)
log_text.insert(tk.END, message + "\n")
log_text.config(state=tk.DISABLED)
log_text.yview(tk.END)
# 创建主窗口
root = tk.Tk()
root.title("Excel图片提取器")
# 创建变量以存储用户输入
excel_path = tk.StringVar()
save_dir = tk.StringVar()
sheet_name = tk.StringVar()
image_column = tk.StringVar()
rename_column = tk.StringVar()
# 创建日志文本框
log_text = scrolledtext.ScrolledText(root, width=50, height=10)
log_text.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
log_text.config(state=tk.DISABLED)
# 创建Excel文件路径区域
excel_path_frame = tk.Frame(root)
excel_path_frame.pack(pady=10)
tk.Label(excel_path_frame, text="Excel文件路径:").pack(side=tk.LEFT)
browse_excel_btn = tk.Button(excel_path_frame, text="浏览...", command=browse_excel_file)
browse_excel_btn.pack(side=tk.RIGHT)
# 创建保存路径区域
save_dir_frame = tk.Frame(root)
save_dir_frame.pack(pady=10)
tk.Label(save_dir_frame, text="保存路径:").pack(side=tk.LEFT)
browse_save_btn = tk.Button(save_dir_frame, text="浏览...", command=browse_save_dir)
browse_save_btn.pack(side=tk.RIGHT)
# 创建工作表名称区域
sheet_name_frame = tk.Frame(root)
sheet_name_frame.pack(pady=10)
tk.Label(sheet_name_frame, text="工作表名称:").pack(side=tk.LEFT)
sheet_name_entry = tk.Entry(sheet_name_frame, textvariable=sheet_name)
sheet_name_entry.pack(side=tk.RIGHT, expand=True, fill=tk.X)
# 创建列选择区域
image_column_frame = tk.Frame(root)
image_column_frame.pack(pady=10)
tk.Label(image_column_frame, text="图片所在列(如: C):").pack(side=tk.LEFT)
image_column_entry = tk.Entry(image_column_frame, textvariable=image_column, width=10)
image_column_entry.pack(side=tk.RIGHT)
rename_column_frame = tk.Frame(root)
rename_column_frame.pack(pady=10)
tk.Label(rename_column_frame, text="重命名值所在列(如: E):").pack(side=tk.LEFT)
rename_column_entry = tk.Entry(rename_column_frame, textvariable=rename_column, width=10)
rename_column_entry.pack(side=tk.RIGHT)
# 创建处理按钮
process_btn = tk.Button(root, text="Excel! 启动!!!", command=process_excel)
process_btn.pack(pady=20)
# 启动事件循环
root.mainloop()