将Excel中的图片链接替换为图片

使用前提:

  • 第一行不能是合并单元格,openpyxl处理会出现问题
  • openpyxl version > 3.0
  • 如果图片地址不是以.jpg结尾,需要更改requests部分代码
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
import requests

wb =load_workbook("example.xlsx")
ws = wb["sheet1"]
file_path = "E:\Jupyter\向Excel中插入图片\img\{}.jpg"

location = []  # 存放坐标列表

for row in ws.rows:
    for cell in row:
        if cell.value and str(cell.value).startswith("http://"):
            location.append(cell.coordinate) # 返回坐标
            pic= requests.get(cell.value)
            cell.value = ""     # 删除原有链接数据
            with open(file_path.format(cell.coordinate),"wb") as f:
                f.write(pic.content)
                
for coordinate in location:   
    file_path = "E:\Jupyter\向Excel中插入图片\img\{}.jpg".format(coordinate)
       
    img = Image(file_path)
    newsize =(112.5,150) # 设置图片大小
    img.width, img.height = newsize
    
    column = "".join(re.findall(r'[A-Za-z]', coordinate)) # 列的位置
    row = int(re.sub("\D", "", coordinate) ) # 行的位置
    
    ws.column_dimensions[column].width = 13.5 # 设置列宽
    ws.row_dimensions[row].height = 114 # 设置行高
    
    ws.add_image(img, coordinate)
    
wb.save("result.xlsx")
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要实现这个功能,你可以使用Selenium和BeautifulSoup库来爬取亚马逊的产品信息,并使用Pandas库将数据保存到Excel表格。下面是一个示例代码: ```python from selenium import webdriver from selenium.webdriver.common.by import By from bs4 import BeautifulSoup import pandas as pd # 设置Chrome浏览器的路径 chrome_path = "path_to_chrome_driver" # 创建一个WebDriver实例 driver = webdriver.Chrome(chrome_path) # 打开亚马逊网站 driver.get("https://www.amazon.com") # 定位搜索框并输入搜索关键词 search_box = driver.find_element(By.ID, "twotabsearchtextbox") search_box.send_keys("your search keyword") search_box.submit() # 等待页面加载完成 driver.implicitly_wait(10) # 获取前十个产品的信息 product_list = driver.find_elements(By.CSS_SELECTOR, ".s-result-item") data = [] for product in product_list[:10]: soup = BeautifulSoup(product.get_attribute("innerHTML"), "html.parser") # 解析产品信息 asin = soup.find("div", {"data-asin": True})["data-asin"] image = soup.find("img")["src"] title = soup.find("span", class_="a-text-normal").get_text(strip=True) rating = soup.find("span", class_="a-icon-alt").get_text(strip=True) price = soup.find("span", class_="a-offscreen").get_text(strip=True) category = soup.find("span", class_="a-size-base a-color-secondary").get_text(strip=True) rank = soup.find("span", class_="s-compact-format").get_text(strip=True) link = "https://www.amazon.com" + soup.find("a", class_="a-link-normal")["href"] # 将产品信息添加到列表 data.append({ "ASIN": asin, "Image": image, "Title": title, "Rating": rating, "Price": price, "Category": category, "Rank": rank, "Link": link }) # 关闭浏览器 driver.quit() # 将数据保存到Excel表格 df = pd.DataFrame(data) df.to_excel("amazon_products.xlsx", index=False) ``` 请将`path_to_chrome_driver`替换为你的Chrome驱动程序的路径。此代码将搜索结果的前十个产品的ASIN、图片链接、标题、评分、价格、小类目、排名和链接保存到名为`amazon_products.xlsx`的Excel表格。你可以根据需求调整代码以获取更多的产品信息或修改保存的文件名。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值