import openpyxl import requests import os filepath =…
- User
import openpyxl import requests import os
file_path = 'D:\images\image2\拜访-2021_01_01-2021_01_31_1.xlsx' new_folder_path = file_path.replace('.xlsx','')
if not os.path.isdir(new_folder_path): os.makedirs(new_folder_path)
打开 Excel 文件
wb = openpyxl.load_workbook(file_path)
sheet_name = '巡逻拍照' # 指定sheet页名称
if sheet_name in wb.sheetnames: # 选择要操作的工作表 sheet = wb[sheet_name] print('行数:'+str(sheet.max_row))
# 循环遍历单元格,查找里面的图片,并进行下载 for row in range(4558, sheet.max_row + 1): for col in range(1, sheet.max_column + 1): cell = sheet.cell(row=row, column=col) # 如果单元格里面有图片 if cell._value and cell._value.startswith('=HYPERLINK'): fpath = new_folder_path + '\\巡逻拍照' + f'{row}-{col}.jpg' if not os.path.exists(fpath): # 下载图片 response = requests.get(cell._value[12:cell._value.index("jpg")+3]) # 将图片保存到本地 with open(fpath, 'wb') as f: f.write(response.content) # 关闭 Excel 文件 wb.close() print(file_path+'下载完成!')优化一下这个代码 - Assistant
以下是不同方面的优化建议:
-
使用os.path.join()方法拼接文件路径,以适应不同操作系统的路径分隔符。
-
使用with语句打开Excel文件,可以省略关闭Excel文件的步骤。
-
对单元格的值进行替换,可以使用正则表达式。
-
将图片下载和保存的过程封装成函数。
-
对错误进行处理,比如文件不存在、文件不能读取等情况。
-
对大量下载图片的程序添加进度条。
综合以上优化建议,可以得到如下代码:
import openpyxl import requests import os import re from tqdm import tqdm # 添加进度条
def download_image(url, save_folder): ''' 下载图片并保存到指定文件夹 ''' try: response = requests.get(url) fname = os.path.basename(url) fpath = os.path.join(save_folder, fname) with open(fpath, 'wb') as f: f.write(response.content) return True except Exception as e: print(f"下载图片时出错:{e}") return False
-
- User
python自己输入文件路径
- Assistant
可以使用 Python 内置的
input()函数获取用户输入的文件路径。以下是一个示例代码:import os import openpyxl import requests import re from tqdm import tqdm def download_image(url, save_folder): ''' 下载图片并保存到指定文件夹 ''' try: response = requests.get(url) fname = os.path.basename(url) fpath = os.path.join(save_folder, fname) with open(fpath, 'wb') as f: f.write(response.content) return True except Exception as e: print(f"下载图片时出错:{e}") return False # 获取用户输入的文件路径 file_path = input("请输入 Excel 文件路径:") # 判断文件是否存在 if not os.path.isfile(file_path): print(f"文件{file_path}不存在!") exit() new_folder_path = os.path.splitext(file_path)[0] if not os.path.isdir(new_folder_path): os.makedirs(new_folder_path) # 打开 Excel 文件 with openpyxl.load_workbook(file_path) as wb: sheet_name = '巡逻拍照' # 指定sheet页名称 if sheet_name in wb.sheetnames: # 选择要操作的工作表 sheet = wb[sheet_name] print('行数:'+str(sheet.max_row)) # 定义正则表达式 pattern = r'=HYPERLINK\("(.+?\.jpg).+' # 循环遍历单元格,查找里面的图片,并进行下载 for row in tqdm(range(1, sheet.max_row + 1)): for col in range(1, sheet.max_column + 1): cell = sheet.cell(row=row, column=col) # 如果单元格里面有图片 if cell.value and re.match(pattern, cell.value): url = re.search(pattern, cell.value).group(1) fpath = os.path.join(new_folder_path, f'巡逻拍照{row}-{col}.jpg') if not os.path.exists(fpath): # 下载图片 download_image(url, new_folder_path) print(file_path + '下载完成!')
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.