You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python从Excel批量下载Google Form收集的CBSE注册学生照片

从Excel存储的链接批量下载学生照片Python实现

需求说明

我已通过Google Form收集CBSE注册所需数据,同时也通过Google Form采集了学生照片。目前需要从导出的Excel文件中批量下载学生照片:照片链接存放在Excel第一列,学生姓名存放在相邻的第二列,第三列用来标记下载状态。

实现代码

import ssl
import sys
import urllib.request
from openpyxl import load_workbook

# 解决SSL证书验证问题
ssl._create_default_https_context = ssl._create_unverified_context

print('正在检查图片文件下载状态......')

# 加载Excel文件,按需修改文件名
wb = load_workbook("Students Details.xlsx")
sheet1 = wb["Sheet1"]
max_row = sheet1.max_row

# 遍历每行数据
for i in range(1, max_row):
    download_status = sheet1.cell(row=i+1, column=3).value
    url = sheet1.cell(row=i+1, column=1).value
    # 保存路径可根据实际情况修改
    savefileas = 'D:/Examination/CBSE/Registration/2021-22/Std.XI/Students Photos/Temp - Photographs/' + sheet1.cell(row=i+1, column=2).value + '.jpg'
        
    if download_status == None:
        a = urllib.request.urlopen(url)
        print(f"请求响应码:{a.getcode()}")
        
        if a.getcode() == 200:
            print('正在连接服务器下载图片')
            urllib.request.urlretrieve(url, savefileas)
            print(f'第{i}条 学生:{sheet1.cell(row=i+1, column=2).value} 照片下载成功')
            sheet1.cell(row=i+1, column=3).value = '下载成功'
            wb.save("Students Details.xlsx")
           
        else:
            print('当前无法下载图片,程序退出')
            sys.exit()
    elif download_status == '下载成功':
        print(f'第{i}条 学生:{sheet1.cell(row=i+1, column=2).value} 已下载过,跳过')

注意事项

  • 原代码缺少import sys导入语句,会导致请求失败调用sys.exit()时报错,已在上述代码中补充
  • 原代码保存路径中冗余的`enter code here`字符串属于误输入,已删除,可根据自己实际存储路径修改对应位置
  • 运行代码前需要先安装openpyxl依赖,执行命令:pip install openpyxl
  • Excel文件名、工作表名如果和示例不一致,需要对应修改代码中的配置

内容的提问来源于stack exchange,提问作者Pankaj Paul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 01:45:02