撰写python代码读取excel中的pdfurl下载pdf文件并输出对应的相对路径地址
以下是一个示例代码,用于读取Excel中的PDF URL,下载PDF文件,并输出对应的相对路径地址:
import pandas as pd
import requests
def download_pdf(pdf_url):
response = requests.get(pdf_url)
if response.status_code == 200:
filename = pdf_url.split("/")[-1]
with open(filename, 'wb') as file:
file.write(response.content)
return filename
else:
return "Failed to download PDF"
# 读取Excel文件
excel_file = pd.read_excel("data.xlsx")
# 获取PDF URL列
pdf_urls = excel_file['PDF URL']
# 下载PDF文件并输出相对路径地址
for pdf_url in pdf_urls:
file_path = download_pdf(pdf_url)
print(file_path)
请确保已经安装了pandas和requests库,可以使用以下命令进行安装:
pip install pandas requests
在代码中,我们定义了一个download_pdf函数,用于下载PDF文件。然后,我们使用pandas库读取Excel文件,并获取PDF URL列。接下来,我们遍历每个PDF URL,调用download_pdf函数进行下载,并将返回的文件路径打印输出。
请替换代码中的data.xlsx为你的Excel文件路径
原文地址: https://www.cveoy.top/t/topic/iClC 著作权归作者所有。请勿转载和采集!