python 读取excel中链接为二进制文件流保存到本地的文件夹内
读取excel内的 表头为 Link 的列
import pandas as pd
import requests
import os
import re
import tkinter as tk
from tkinter import filedialog, messagebox
def browse_file():
file_path = filedialog.askopenfilename()
if file_path:
entry_path.delete(0, tk.END)
entry_path.insert(0, file_path)
def download_images():
# 获取文件路径
excel_path = entry_path.get()
# 读取 Excel 文件
df = pd.read_excel(excel_path)
# 创建目录用于保存图片
if not os.path.exists("images"):
os.mkdir("images")
# 循环处理每个链接并保存对应的图片文件
for i, row in df.iterrows():
link = row["Link"]
if pd.notna(link):
match = re.search(r"\bcodes=(\d+)\b", link)
if match:
codes = match.group(1)
else:
continue
# 发起 HTTP 请求并保存响应数据到本地文件
response = requests.get(link)
filename = f"image_{codes}.png"
filepath = os.path.join("images", filename)
with open(filepath, "wb") as f:
f.write(response.content)
# 提示下载完成
messagebox.showinfo(title="提示", message="图片下载完成!")
# 创建主窗口
window = tk.Tk()
window.title("图片下载器")
window.geometry("600x200")
# 创建文件选择控件
frame_file = tk.Frame(window)
frame_file.pack(pady=10)
label_path = tk.Label(frame_file, text="请选择 Excel 文件:", font=("微软雅黑", 12))
label_path.pack(side=tk.LEFT)
entry_path = tk.Entry(frame_file, width=30, font=("微软雅黑", 12))
entry_path.pack(side=tk.LEFT)
button_browse = tk.Button(frame_file, text="浏览", font=("微软雅黑", 12), command=browse_file)
button_browse.pack(side=tk.LEFT)
# 创建下载按钮
button_download = tk.Button(window, text="下载图片", font=("微软雅黑", 16), command=download_images)
button_download.pack(pady=20)
# 进入消息循环
window.mainloop()