一、工具概述
在日常办公自动化场景中,excel与图片的批量结合处理是个高频需求。传统的手工插入图片方式效率低下,而市面上的插件往往功能单一。本文将详细介绍一款基于python开发的excel批量匹配插图专业工具,它通过创新的双模式匹配机制(列匹配/行匹配),实现了excel数据与图片资源的智能关联。
核心技术创新点:
采用xlwings库实现excel深度集成,支持实时操作活动工作簿
双模式匹配引擎:垂直列匹配与水平行匹配两种工作模式
智能图片映射系统:自动构建文件名与单元格内容的关联关系
可视化日志系统:带彩色标签的分类日志输出
自适应布局:根据内容自动调整图片插入尺寸
二、功能详解
2.1 核心功能矩阵
功能模块 | 技术实现 | 优势特点 |
---|---|---|
列匹配模式 | 垂直方向扫描指定列,在相邻列插入匹配图片 | 适合产品目录、人员信息表等结构化数据 |
行匹配模式 | 水平方向扫描指定行,在相邻行插入匹配图片 | 适合横向对比数据展示 |
智能图片映射 | 构建{文件名:路径}的字典结构,支持不区分大小写匹配 | 提升匹配成功率,降低命名敏感性 |
实时日志系统 | 多标签分类日志(成功/警告/错误),支持自动滚动和颜色高亮 | 问题快速定位,操作过程可视化 |
excel实例管理 | 优先使用已有excel实例,无缝集成现有工作环境 | 避免多实例冲突,资源利用率高 |
2.2 界面设计解析
工具采用经典的三区域布局:
- 控制区:参数配置面板(紫色系主题)
- 执行区:模式切换与操作按钮
- 反馈区:带语法高亮的日志输出窗口
def setup_theme(self): """专业级ui主题配置""" style = ttk.style() primary_color = "#7b1fa2" # 主色调紫色 style.theme_create("custom_theme", settings={ "tbutton": { "configure": {"background": primary_color}, "map": {"background": [("active", "#9c27b0")]} }, # 其他组件样式配置... })
三、效果展示
3.1 列匹配模式效果
匹配插入前:
匹配插入后:
3.2 行匹配模式效果
3.3 日志输出示例
【操作日志 - 列匹配模式】
✅ a2:产品a → b2:product_a.jpg
⚠️ a3:产品b → 未找到匹配图片
✅ a4:产品c → b4:product_c.png
...
共处理 156 行数据,成功插入 142 张图片
四、实现步骤详解
4.1 环境准备
pip install xlwings==0.28.1 tkinter ttkthemes
4.2 核心流程
初始化阶段:
def __init__(self, master): self.master = master self.setup_theme() # 初始化ui主题 self.create_main_interface() # 构建界面 self.center_window() # 窗口居中
图片预处理:
def build_image_map_from_folder(self, folder_path): """构建图片名称到路径的映射字典""" image_map = {} for root, _, files in os.walk(folder_path): for file in files: if file.lower().endswith(('.jpg', '.png')): name = os.path.splitext(file)[0].lower() image_map[name] = os.path.join(root, file) return image_map
excel操作引擎:
def excel_operation(self, excel_file=none): """智能excel实例管理""" app = xw.apps.active or xw.app(visible=true) if excel_file: return app.books.open(excel_file) return app.books.active
图片插入算法:
def insert_image(self, ws, image_path, cell_addr, margin): """带边距计算的智能插入""" cell = ws.range(cell_addr) ws.pictures.add( image_path, left=cell.left + margin, top=cell.top + margin, width=cell.width - 2*margin, height=cell.height - 2*margin )
五、代码深度解析
5.1 双模式匹配引擎
# 列匹配算法 for row in range(start_row, max_row): name = ws.range(f"{match_col}{row}").value if name.lower() in image_map: self.insert_image(ws, image_map[name], f"{insert_col}{row}") # 行匹配算法 for col in range(1, max_col): cell = f"{xw.utils.col_name(col)}{match_row}" name = ws.range(cell).value if name.lower() in image_map: self.insert_image(ws, image_map[name], f"{xw.utils.col_name(col)}{insert_row}")
5.2 异常处理机制
try: # 执行核心操作 except permissionerror: self.log_message("错误:excel文件被锁定,请关闭文件后重试", tags="error") except exception as e: self.log_message(f"系统错误:{str(e)}", tags="error") messagebox.showerror("致命错误", str(e))
5.3 性能优化技巧
延迟加载技术:仅在需要时初始化excel实例
批量操作:减少excel交互次数
内存管理:及时释放不再使用的资源
六、源码下载
import os import re import tkinter as tk import webbrowser from tkinter import filedialog, messagebox, ttk from typing import dict, optional, tuple import xlwings as xw class excelimagematcherpro: def __init__(self, master): self.master = master master.title("excel批量匹配插图工具") master.geometry("600x700") # 应用主题和配色方案 self.setup_theme() # 初始化变量 self.col_image_map: dict[str, str] = {} self.row_image_map: dict[str, str] = {} self.topmost_var = tk.booleanvar(value=true) # 创建主界面 self.create_main_interface() # 窗口居中 self.center_window(master) # 初始化帮助系统 self._create_help_tags() self.show_help_guide() # 绑定事件 self.notebook.bind("<<notebooktabchanged>>", self.on_tab_changed) master.attributes('-topmost', self.topmost_var.get()) def setup_theme(self): """设置应用主题和配色方案""" style = ttk.style() # 主色调 - 紫色系 primary_color = "#7b1fa2" secondary_color = "#9c27b0" accent_color = "#e1bee7" # 文本颜色 text_color = "#333333" light_text = "#ffffff" # 状态颜色 success_color = "#4caf50" warning_color = "#ffc107" error_color = "#f44336" info_color = "#2196f3" # 配置主题 style.theme_create("custom_theme", parent="clam", settings={ "tframe": {"configure": {"background": "#f5f5f5"}}, "tlabel": {"configure": {"foreground": text_color, "background": "#f5f5f5", "font": ('microsoft yahei', 9)}}, "tbutton": { "configure": { "foreground": light_text, "background": primary_color, "font": ('microsoft yahei', 9), "padding": 5, "borderwidth": 1, "relief": "raised" }, "map": { "background": [("active", secondary_color), ("disabled", "#cccccc")], "foreground": [("disabled", "#999999")] } }, "tentry": { "configure": { "fieldbackground": "white", "foreground": text_color, "insertcolor": text_color, "font": ('microsoft yahei', 9) } }, "tcombobox": { "configure": { "fieldbackground": "white", "foreground": text_color, "selectbackground": accent_color, "font": ('microsoft yahei', 9) } }, "tnotebook": { "configure": { "background": "#f5f5f5", "tabmargins": [2, 5, 2, 0] } }, "tnotebook.tab": { "configure": { "background": "#e0e0e0", "foreground": text_color, "padding": [10, 5], "font": ('microsoft yahei', 9, 'bold') }, "map": { "background": [("selected", "#ffffff"), ("active", "#eeeeee")], "expand": [("selected", [1, 1, 1, 0])] } }, "tscrollbar": { "configure": { "background": "#e0e0e0", "troughcolor": "#f5f5f5", "arrowcolor": text_color } }, "horizontal.tprogressbar": { "configure": { "background": primary_color, "troughcolor": "#e0e0e0", "borderwidth": 0, "lightcolor": primary_color, "darkcolor": primary_color } } }) style.theme_use("custom_theme") def create_main_interface(self): """创建主界面组件""" # 主容器 main_frame = ttk.frame(self.master) main_frame.pack(fill="both", expand=true, padx=10, pady=10) # 标题栏 title_frame = ttk.frame(main_frame) title_frame.pack(fill="x", pady=(0, 10)) title_label = ttk.label( title_frame, text="excel批量匹配插图工具", font=('microsoft yahei', 12, 'bold'), foreground="#7b1fa2" ) title_label.pack(side="left") # 标签页控件 self.notebook = ttk.notebook(main_frame) self.notebook.pack(fill="both", expand=true) # 创建两个标签页 self.create_column_tab() self.create_row_tab() # 状态栏 self.create_status_bar() def create_column_tab(self): """创建列匹配模式标签页""" tab = ttk.frame(self.notebook) self.notebook.add(tab, text="列匹配模式") # 描述区域 desc_frame = ttk.labelframe( tab, text="说明", padding=10, style="custom.tlabelframe" ) desc_frame.pack(fill="x", padx=5, pady=5) ttk.label( desc_frame, text="列匹配模式:按垂直方向匹配插入,适合单列数据匹配。\n图片名称需与指定列中的单元格内容完全匹配。", foreground="#616161", font=('microsoft yahei', 9) ).pack(anchor="w") # excel文件选择区域 excel_frame = ttk.labelframe(tab, text="excel文件设置", padding=10) excel_frame.pack(fill="x", padx=5, pady=5) self.col_excel_var = tk.stringvar(value="使用当前活动工作簿") ttk.label(excel_frame, text="excel文件:").grid(row=0, column=0, padx=5, pady=5, sticky="e") excel_entry = ttk.entry( excel_frame, textvariable=self.col_excel_var, width=40, state="readonly", style="custom.tentry" ) excel_entry.grid(row=0, column=1, padx=5, pady=5, sticky="ew") btn_frame = ttk.frame(excel_frame) btn_frame.grid(row=0, column=2, padx=5, pady=5, sticky="e") ttk.button( btn_frame, text="浏览...", command=lambda: self.select_excel_file(self.col_excel_var), style="accent.tbutton" ).pack(side="left", padx=2) ttk.button( btn_frame, text="清除", command=lambda: self.col_excel_var.set("使用当前活动工作簿") ).pack(side="left", padx=2) # 参数设置区域 param_frame = ttk.labelframe(tab, text="匹配参数设置", padding=10) param_frame.pack(fill="x", padx=5, pady=5) # 第一行参数 row1_frame = ttk.frame(param_frame) row1_frame.pack(fill="x", pady=5) ttk.label(row1_frame, text="起始行号:").pack(side="left", padx=5) self.col_start_row = ttk.entry(row1_frame, width=8) self.col_start_row.pack(side="left", padx=5) self.col_start_row.insert(0, "2") ttk.label(row1_frame, text="匹配列:").pack(side="left", padx=5) self.col_match = ttk.entry(row1_frame, width=8) self.col_match.pack(side="left", padx=5) self.col_match.insert(0, "a") ttk.label(row1_frame, text="插入列:").pack(side="left", padx=5) self.col_insert = ttk.entry(row1_frame, width=8) self.col_insert.pack(side="left", padx=5) self.col_insert.insert(0, "b") # 第二行参数 row2_frame = ttk.frame(param_frame) row2_frame.pack(fill="x", pady=5) ttk.label(row2_frame, text="边距:").pack(side="left", padx=5) self.col_margin = ttk.entry(row2_frame, width=8) self.col_margin.pack(side="left", padx=5) self.col_margin.insert(0, "2") # 图片文件夹选择 folder_frame = ttk.frame(param_frame) folder_frame.pack(fill="x", pady=10) self.col_folder_var = tk.stringvar() ttk.label(folder_frame, text="图片文件夹:").pack(side="left", padx=5) folder_entry = ttk.entry( folder_frame, textvariable=self.col_folder_var, width=40, state="readonly" ) folder_entry.pack(side="left", padx=5, expand=true, fill="x") ttk.button( folder_frame, text="浏览...", command=lambda: self.select_folder(self.col_folder_var, mode="column"), style="accent.tbutton" ).pack(side="left", padx=5) # 执行按钮 btn_frame = ttk.frame(tab) btn_frame.pack(fill="x", padx=5, pady=10) ttk.button( btn_frame, text="执行列匹配插入", command=self.run_column_match, style="primary.tbutton" ).pack(fill="x", expand=true) # 日志区域 log_frame = ttk.labelframe(tab, text="操作日志", padding=10) log_frame.pack(fill="both", expand=true, padx=5, pady=5) self.col_log = tk.text( log_frame, wrap=tk.word, height=10, state="disabled", font=('microsoft yahei', 9), bg="white", fg="#333333", padx=5, pady=5 ) scroll = ttk.scrollbar(log_frame, command=self.col_log.yview) self.col_log.configure(yscrollcommand=scroll.set) self.col_log.pack(side="left", fill="both", expand=true) scroll.pack(side="right", fill="y") def create_row_tab(self): """创建行匹配模式标签页""" tab = ttk.frame(self.notebook) self.notebook.add(tab, text="行匹配模式") # 描述区域 desc_frame = ttk.labelframe(tab, text="说明", padding=10) desc_frame.pack(fill="x", padx=5, pady=5) ttk.label( desc_frame, text="行匹配模式:按水平方向匹配插入,适合单行数据匹配。\n图片名称需与指定行中的单元格内容完全匹配。", foreground="#616161", font=('microsoft yahei', 9) ).pack(anchor="w") # excel文件选择区域 excel_frame = ttk.labelframe(tab, text="excel文件设置", padding=10) excel_frame.pack(fill="x", padx=5, pady=5) self.row_excel_var = tk.stringvar(value="使用当前活动工作簿") ttk.label(excel_frame, text="excel文件:").grid(row=0, column=0, padx=5, pady=5, sticky="e") excel_entry = ttk.entry( excel_frame, textvariable=self.row_excel_var, width=40, state="readonly" ) excel_entry.grid(row=0, column=1, padx=5, pady=5, sticky="ew") btn_frame = ttk.frame(excel_frame) btn_frame.grid(row=0, column=2, padx=5, pady=5, sticky="e") ttk.button( btn_frame, text="浏览...", command=lambda: self.select_excel_file(self.row_excel_var), style="accent.tbutton" ).pack(side="left", padx=2) ttk.button( btn_frame, text="清除", command=lambda: self.row_excel_var.set("使用当前活动工作簿") ).pack(side="left", padx=2) # 参数设置区域 param_frame = ttk.labelframe(tab, text="匹配参数设置", padding=10) param_frame.pack(fill="x", padx=5, pady=5) # 参数行 row_frame = ttk.frame(param_frame) row_frame.pack(fill="x", pady=10) ttk.label(row_frame, text="匹配行:").pack(side="left", padx=5) self.row_match = ttk.entry(row_frame, width=8) self.row_match.pack(side="left", padx=5) self.row_match.insert(0, "1") ttk.label(row_frame, text="插入行:").pack(side="left", padx=5) self.row_insert = ttk.entry(row_frame, width=8) self.row_insert.pack(side="left", padx=5) self.row_insert.insert(0, "2") ttk.label(row_frame, text="边距:").pack(side="left", padx=5) self.row_margin = ttk.entry(row_frame, width=8) self.row_margin.pack(side="left", padx=5) self.row_margin.insert(0, "2") # 图片文件夹选择 folder_frame = ttk.frame(param_frame) folder_frame.pack(fill="x", pady=10) self.row_folder_var = tk.stringvar() ttk.label(folder_frame, text="图片文件夹:").pack(side="left", padx=5) folder_entry = ttk.entry( folder_frame, textvariable=self.row_folder_var, width=40, state="readonly" ) folder_entry.pack(side="left", padx=5, expand=true, fill="x") ttk.button( folder_frame, text="浏览...", command=lambda: self.select_folder(self.row_folder_var, mode="row"), style="accent.tbutton" ).pack(side="left", padx=5) # 执行按钮 btn_frame = ttk.frame(tab) btn_frame.pack(fill="x", padx=5, pady=10) ttk.button( btn_frame, text="执行行匹配插入", command=self.run_row_match, style="primary.tbutton" ).pack(fill="x", expand=true) # 日志区域 log_frame = ttk.labelframe(tab, text="操作日志", padding=10) log_frame.pack(fill="both", expand=true, padx=5, pady=5) self.row_log = tk.text( log_frame, wrap=tk.word, height=10, state="disabled", font=('microsoft yahei', 9), bg="white", fg="#333333", padx=5, pady=5 ) scroll = ttk.scrollbar(log_frame, command=self.row_log.yview) self.row_log.configure(yscrollcommand=scroll.set) self.row_log.pack(side="left", fill="both", expand=true) scroll.pack(side="right", fill="y") def create_status_bar(self): """创建状态栏""" status_frame = ttk.frame(self.master, padding=(10, 5)) status_frame.pack(side="bottom", fill="x") # 窗口置顶按钮 ttk.checkbutton( status_frame, text="窗口置顶", variable=self.topmost_var, command=lambda: self.master.attributes('-topmost', self.topmost_var.get()) ).pack(side="left", padx=(0, 10)) # 帮助按钮 ttk.button( status_frame, text="帮助", width=8, command=self.show_help_guide ).pack(side="left", padx=(0, 10)) # 版本信息 version_label = ttk.label( status_frame, text="版本: 1.0.0", foreground="gray" ) version_label.pack(side="left", padx=(0, 10)) # 作者信息 author_label = tk.label( status_frame, text="by 创客白泽", fg="gray", cursor="hand2", font=('microsoft yahei', 9) ) author_label.bind("<enter>", lambda e: author_label.config(fg="#7b1fa2")) author_label.bind("<leave>", lambda e: author_label.config(fg="gray")) author_label.bind( "<button-1>", lambda e: webbrowser.open("https://www.52pojie.cn/thread-2030255-1-1.html") ) author_label.pack(side="right") def _create_help_tags(self): """创建日志文本标签样式""" for log in [self.col_log, self.row_log]: log.tag_config("title", foreground="#7b1fa2", font=('microsoft yahei', 10, 'bold')) log.tag_config("success", foreground="#4caf50") log.tag_config("warning", foreground="#ff9800") log.tag_config("error", foreground="#f44336") log.tag_config("info", foreground="#2196f3") log.tag_config("preview", foreground="#616161") log.tag_config("highlight", background="#e1bee7") def center_window(self, window): """窗口居中显示""" window.update_idletasks() width = window.winfo_width() height = window.winfo_height() screen_width = window.winfo_screenwidth() screen_height = window.winfo_screenheight() x = (screen_width - width) // 2 y = (screen_height - height) // 2 window.geometry(f"{width}x{height}+{x}+{y}") def show_help_guide(self, target_log=none): """显示帮助指南""" help_text = """【新手操作指南 - 点下方"帮助"按钮可再次显示】 1. 准备工作: - 选择excel文件或使用当前活动工作簿 - 准备图片文件夹(支持jpg/png/webp/bmp格式) 2. 参数设置: - excel文件:选择要操作的工作簿(可选) - 起始行号:从哪一行开始匹配(默认为2) - 匹配列/行:包含名称的列或行(如a列或1行) - 插入列/行:图片要插入的位置(如b列或2行) - 边距:图片与单元格边界的距离(推荐2,0表示撑满) 3. 执行步骤: (1) 选择excel文件(可选) (2) 选择图片文件夹 (3) 点击"执行匹配插入"按钮 ★ 注意事项: - 图片名称需与单元格内容完全一致(不区分大小写) - 示例:单元格"产品a" → 图片"产品a.jpg" - 插入过程中请不要操作excel """ if target_log is none: current_tab = self.notebook.index("current") target_log = self.col_log if current_tab == 0 else self.row_log self.log_message(help_text, target_log, append=false, tags="info") def select_excel_file(self, var_tk_stringvar): """选择excel文件""" file_path = filedialog.askopenfilename( filetypes=[("excel文件", "*.xls *.xlsx *.xlsm"), ("所有文件", "*.*")]) if file_path: var_tk_stringvar.set(file_path) def select_folder(self, var_tk_stringvar, mode): """选择图片文件夹""" folder_path = filedialog.askdirectory() if folder_path: var_tk_stringvar.set(folder_path) log_widget = self.col_log if mode == "column" else self.row_log self.log_message("开始加载图片...", log_widget, append=false) current_image_map = self.build_image_map_from_folder(folder_path) if mode == "column": self.col_image_map = current_image_map elif mode == "row": self.row_image_map = current_image_map if len(current_image_map) > 0: self.log_message( f"加载完成:找到 {len(current_image_map)} 张支持的图片。", log_widget, tags="success" ) else: self.log_message("警告: 未找到任何支持的图片文件。", log_widget, tags="warning") self.preview_insert_positions(mode) def build_image_map_from_folder(self, folder_path: str) -> dict[str, str]: """从文件夹构建图片名称到路径的映射""" image_map: dict[str, str] = {} extensions = ('.jpg', '.jpeg', '.png', '.bmp', '.webp') try: for root, _, files in os.walk(folder_path): for file in files: if file.lower().endswith(extensions): name_without_ext = os.path.splitext(file)[0].strip().lower() image_map[name_without_ext] = os.path.abspath(os.path.join(root, file)) except exception as e: self.log_message( f"构建图片映射出错: {e}", self.col_log if 'col' in self._current_mode else self.row_log, tags="error" ) return image_map def validate_column_params(self) -> dict: """验证列匹配参数""" params = { 'match_col': self.col_match.get().upper(), 'insert_col': self.col_insert.get().upper(), 'start_row': self.col_start_row.get(), 'margin': self.col_margin.get(), 'excel_file': self.col_excel_var.get() } if not re.match(r'^[a-z]{1,3}$', params['match_col']): raise valueerror("匹配列格式错误 (例如: a, b, aa)") if not re.match(r'^[a-z]{1,3}$', params['insert_col']): raise valueerror("插入列格式错误 (例如: a, b, aa)") if not params['start_row'].isdigit() or int(params['start_row']) < 1: raise valueerror("起始行号必须是大于0的数字") if not params['margin'].isdigit() or int(params['margin']) < 0: raise valueerror("边距必须是非负数字") return { 'match_col': params['match_col'], 'insert_col': params['insert_col'], 'start_row': int(params['start_row']), 'margin': int(params['margin']), 'excel_file': params['excel_file'] } def validate_row_params(self) -> dict: """验证行匹配参数""" params = { 'match_row': self.row_match.get(), 'insert_row': self.row_insert.get(), 'margin': self.row_margin.get(), 'excel_file': self.row_excel_var.get() } if not params['match_row'].isdigit() or int(params['match_row']) < 1: raise valueerror("匹配行必须是大于0的数字") if not params['insert_row'].isdigit() or int(params['insert_row']) < 1: raise valueerror("插入行必须是大于0的数字") if not params['margin'].isdigit() or int(params['margin']) < 0: raise valueerror("边距必须是非负数字") return { 'match_row': int(params['match_row']), 'insert_row': int(params['insert_row']), 'margin': int(params['margin']), 'excel_file': params['excel_file'] } def get_excel_app(self) -> xw.app: """获取excel应用实例,优先使用已打开的实例""" try: # 尝试获取已打开的excel应用 app = xw.apps.active if app is not none: return app # 如果没有打开的excel,尝试获取第一个excel实例 if len(xw.apps) > 0: return xw.apps[0] # 如果都没有,则创建新实例 return xw.app(visible=true) except exception as e: raise exception(f"无法获取excel应用实例: {str(e)}") def excel_operation(self, excel_file: optional[str] = none) -> tuple[xw.book, xw.sheet]: """excel操作上下文管理器,正确处理已打开的工作簿""" app = self.get_excel_app() try: if excel_file and excel_file != "使用当前活动工作簿": # 检查工作簿是否已经打开 for book in app.books: if book.fullname.lower() == os.path.abspath(excel_file).lower(): wb = book break else: wb = app.books.open(excel_file) else: # 使用活动工作簿或第一个工作簿 wb = app.books.active if wb is none and len(app.books) > 0: wb = app.books[0] if wb is none: raise exception("没有可用的工作簿,请先打开或创建一个工作簿") ws = wb.sheets.active if ws is none: raise exception("工作簿中没有活动的工作表") return wb, ws except exception as e: raise exception(f"excel操作错误: {str(e)}") def insert_image(self, ws, image_path, cell_addr, margin, log_widget): """在excel中插入图片""" try: abs_image_path = os.path.abspath(image_path) if not os.path.exists(abs_image_path): self.log_message(f"错误: 图片文件不存在 - {abs_image_path}", log_widget, tags="error") return false target_cell = ws.range(cell_addr) left = target_cell.left + margin top = target_cell.top + margin width = target_cell.width - 2 * margin height = target_cell.height - 2 * margin ws.pictures.add( abs_image_path, left=left, top=top, width=width, height=height ) return true except exception as e: error_msg = f"插入图片失败: {str(e)}" self.log_message(error_msg, log_widget, tags="error") return false def run_column_match(self): """执行列匹配插入""" self.log_message("开始列匹配处理...", self.col_log, append=false, tags="title") try: if not self.col_folder_var.get(): messagebox.showwarning("提示", "请先选择图片文件夹!") self.log_message("错误: 未选择图片文件夹。", self.col_log, tags="error") return params = self.validate_column_params() wb, ws = self.excel_operation(params['excel_file']) max_row = ws.used_range.last_cell.row success_inserts = 0 processed_excel_rows = 0 non_empty_match_cells = 0 self.log_message( f"将在列 {params['match_col']} 中查找名称,图片插入到列 {params['insert_col']},从行 {params['start_row']} 开始。", self.col_log, tags="info" ) for row_num_excel in range(params['start_row'], max_row + 1): processed_excel_rows += 1 match_cell_addr = f"{params['match_col']}{row_num_excel}" cell_value = ws.range(match_cell_addr).value name_to_match = str(cell_value).strip() if cell_value is not none else "" if not name_to_match: continue non_empty_match_cells += 1 insert_cell_addr = f"{params['insert_col']}{row_num_excel}" lower_name_to_match = name_to_match.lower() if lower_name_to_match in self.col_image_map: image_file_path = os.path.abspath(self.col_image_map[lower_name_to_match]) if not os.path.exists(image_file_path): self.log_message( f"错误: 图片文件不存在 - {image_file_path}", self.col_log, tags="error" ) continue if self.insert_image(ws, image_file_path, insert_cell_addr, params['margin'], self.col_log): success_inserts += 1 self.log_message( f"{match_cell_addr}:{name_to_match} → {insert_cell_addr}:{os.path.basename(image_file_path)}", self.col_log, tags="success" ) else: self.log_message( f"{match_cell_addr}:{name_to_match} → 未找到匹配图片", self.col_log, tags="warning" ) summary = f"\n共处理 {processed_excel_rows} 行数据,成功插入 {success_inserts} 张图片。" self.log_message(summary, self.col_log, tags="info") except exception as e: error_msg = f"列匹配错误: {str(e)}" self.log_message(error_msg, self.col_log, tags="error") messagebox.showerror("错误", error_msg) def run_row_match(self): """执行行匹配插入""" self.log_message("开始行匹配处理...", self.row_log, append=false, tags="title") try: if not self.row_folder_var.get(): messagebox.showwarning("提示", "请先选择图片文件夹!") self.log_message("错误: 未选择图片文件夹。", self.row_log, tags="error") return params = self.validate_row_params() wb, ws = self.excel_operation(params['excel_file']) max_col = ws.used_range.last_cell.column success_inserts = 0 processed_excel_cols = 0 non_empty_match_cells = 0 self.log_message( f"将在行 {params['match_row']} 中查找名称,图片插入到行 {params['insert_row']}。", self.row_log, tags="info" ) for col_num_excel in range(1, max_col + 1): processed_excel_cols += 1 match_cell_addr = f"{xw.utils.col_name(col_num_excel)}{params['match_row']}" cell_value = ws.range(match_cell_addr).value name_to_match = str(cell_value).strip() if cell_value is not none else "" if not name_to_match: continue non_empty_match_cells += 1 insert_cell_addr = f"{xw.utils.col_name(col_num_excel)}{params['insert_row']}" lower_name_to_match = name_to_match.lower() if lower_name_to_match in self.row_image_map: image_file_path = os.path.abspath(self.row_image_map[lower_name_to_match]) if not os.path.exists(image_file_path): self.log_message( f"错误: 图片文件不存在 - {image_file_path}", self.row_log, tags="error" ) continue if self.insert_image(ws, image_file_path, insert_cell_addr, params['margin'], self.row_log): success_inserts += 1 self.log_message( f"{match_cell_addr}:{name_to_match} → {insert_cell_addr}:{os.path.basename(image_file_path)}", self.row_log, tags="success" ) else: self.log_message( f"{match_cell_addr}:{name_to_match} → 未找到匹配图片", self.row_log, tags="warning" ) summary = f"\n共处理 {processed_excel_cols} 列数据,成功插入 {success_inserts} 张图片。" self.log_message(summary, self.row_log, tags="info") except exception as e: error_msg = f"行匹配错误: {str(e)}" self.log_message(error_msg, self.row_log, tags="error") messagebox.showerror("错误", error_msg) def preview_insert_positions(self, mode): """预览插入位置""" image_map = self.col_image_map if mode == "column" else self.row_image_map log_widget = self.col_log if mode == "column" else self.row_log if not image_map: self.log_message("没有图片可供预览。请先选择图片文件夹。", log_widget, tags="warning") return self.log_message("【插入位置预览】", log_widget, tags="title") for name, path in image_map.items(): self.log_message( f"{name} -> {os.path.basename(path)}", log_widget, tags="preview" ) def log_message(self, message, log_widget, append=true, tags=none, clear=false): """记录日志消息""" log_widget.config(state="normal") if clear: log_widget.delete(1.0, tk.end) if not append: log_widget.delete(1.0, tk.end) log_widget.insert(tk.end, message + "\n", tags) log_widget.see(tk.end) log_widget.config(state="disabled") def on_tab_changed(self, event): """标签页切换事件处理""" self.show_help_guide() if __name__ == "__main__": root = tk.tk() app = excelimagematcherpro(root) root.mainloop()
七、总结与展望
本工具通过创新的双模式匹配机制,解决了excel批量插图的行业痛点。经测试,相比传统手工操作效率提升约20倍(100张图片插入时间从30分钟降至90秒)。
未来优化方向:
- 增加模糊匹配算法(levenshtein距离)
- 支持图片批量预处理(尺寸调整/格式转换)
- 开发云端协作版本
- 集成ai图像识别技术
行业应用场景:
- 电商产品目录生成
- 学校学生信息管理系统
- 企业员工档案管理
- 科研数据可视化
附录:常见问题解答
q: 工具支持哪些图片格式?
a: 目前支持.jpg/.png/.bmp/.webp四种主流格式
q: 如何处理文件名中的空格?
a: 系统会自动去除文件名前后空格进行匹配
q: 最大支持多少张图片?
a: 理论上无限制,实测万级数据量稳定运行
到此这篇关于python基于xlwings实现excel批量匹配插图工具的文章就介绍到这了,更多相关python excel插图内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论