既然这样,那就自己搞一个吧[Python] 纯文本查看 复制代码import openpyxl
from PIL import Image as PillowImage
import os
from datetime import datetime
"""
将excel表中的图片以前3列的数据+时间戳命名
"""
def get_current_timestamp():
return datetime.now().strftime('%Y%m%d%H%M%S%f')[:-3]
def export_excel_images(excel_file_path, file_path, naming_columns=(0, 1, 2)):
workbook = openpyxl.load_workbook(excel_file_path)
if not os.path.exists(file_path):
os.makedirs(file_path)
for sheet_name in workbook.sheetnames:
worksheet = workbook[sheet_name]
for img in worksheet._images:
try:
anchor_row = img.anchor._from.row + 1
name_parts = []
for col_index in naming_columns:
cell_value = worksheet.cell(row=anchor_row, column=col_index + 1).value
name_parts.append(str(cell_value) if cell_value is not None else "")
timestamp_part = get_current_timestamp()
name = '_'.join(name_parts) + '_' + timestamp_part
image_path = os.path.join(file_path, f"{name}.jpeg")
pil_image = PillowImage.open(img.ref)
pil_image.save(image_path)
print(f"成功保存图片: {image_path}")
except AttributeError as ae:
print(f"保存图片时出现属性错误: {ae},请检查图片相关数据是否正确。")
except Exception as e:
print(f"保存图片时出现未知错误: {e}")
excel_file_path = r"C:\***\****.xlsx"#excel表格路径
file_path =r"C:\****\****"#图片保存地址
export_excel_images(excel_file_path, file_path)