使用 Python 在 Excel 中插入、压缩、替换、提取和删除图像
off999 2025-04-30 18:49 31 浏览 0 评论
Excel 不仅仅是一个数字和公式的工具;它还提供了用于处理图像的强大功能。无论您是创建报告、演示文稿还是数据可视化,合并图像都可以大大提高 Excel 项目的整体影响力和专业性。
在 Excel 中处理图像的 Python 库
要在 Excel 中插入、压缩、替换、提取和删除图像,我们将使用 Spire.XLS for Python 库。
可以通过在终端中运行以下命令从 PyPI 安装 Spire.XLS for Python:
pip install Spire.Xls有关安装的更多详细信息,可以查看以下官方文档:
- 如何在 Windows 上安装 Spire.XLS for Python
- 如何在 Mac 上安装 Spire.XLS for Python
使用 Python 在 Excel 单元格中插入图像
将图像集成到电子表格中可以通过提供视觉上下文和支持信息来增强数据的整体呈现和交流。
Spire.XLS for Python 中的 Worksheet.Pictures.Add() 函数用于将图像插入 Excel 单元格。这是一个简单的示例,展示了我们如何使用此功能将本地文件中的图像插入 Excel 单元格:
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Specify the cell to add the image
range = sheet.Range["C2"]
# Specify the path to the image you want to insert
image_path = "Medium.png"
# Insert the image into the worksheet
image = sheet.Pictures.Add(range.Row, range.Column, image_path)
# Set the properties of the image
image.ResizeBehave = ResizeBehaveType.MoveNotResize # move but don't size with cells
# Set the left column and top row offsets for the image
image.LeftColumnOffset = 25
image.TopRowOffset = 25
# Set the height of the row where the cell is located
sheet.SetRowHeight(range.Row, 35)
# Set the width of the column where the cell is located
sheet.SetColumnWidth(range.Column, 17)
# Save the resulting file
workbook.SaveToFile("InsertLocalImage.xlsx", FileFormat.Version2013)
workbook.Dispose()使用 Python 在 Excel 单元格中插入在线图像
除了将本地文件中的图像插入 Excel 之外,您还可以从外部 Web 链接插入在线图像。使用 Spire.XLS for Python 有两种方法可以实现此目的:
1. 链接到外部 Web 资源:
- 使用 Spire.XLS 提供的 Worksheet.Pictures.AddLinkPic() 函数。
- 使用此方法时,图像不会嵌入到 Excel 文件中。相反,它指向外部 Web 资源。
- 在 Microsoft Excel 中打开电子表格时,应用程序将自动从指定的 URL 下载图像并将其显示在工作表中。
此方法的优点:
- 较小的文件大小:Excel 文件大小保持较小,因为图像数据未存储在文件中。
- 动态更新:如果外部 URL 中的图像已更新,则更改将在打开 Excel 电子表格时反映在 Excel 电子表格中。
- 降低存储要求:Excel 文件不需要存储图像数据,这对于大型或高分辨率图像非常有用。
可能的缺点:
- 对外部资源的依赖:仅当外部 URL 可访问时,才会显示图像。如果图像被移动或 URL 发生更改,图像可能无法在电子表格中正确显示。
- 性能:根据 Internet 连接速度,每次打开电子表格时频繁地从外部 URL 下载图像可能会影响电子表格的性能和加载时间。
以下示例显示了如何通过链接到外部 Web 资源将在线图像插入 Excel:
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Specify the cell to add the image
range = sheet.Range["C2"]
# Specify the URL of the image you want to insert
image_url = "https://i.ibb.co/dJ8h49F/Medium.png"
# Specify image width and height
image_width = 35
image_height = 116
# Link to the external image resource
image = sheet.Pictures.AddLinkPic(range.Row, range.Column, image_width, image_height, image_url)
# Set the properties of the image to “move but don't size with cells”
image.ResizeBehave = ResizeBehaveType.MoveNotResize
# Set the left column and top row offsets for the image
image.LeftColumnOffset = 25
image.TopRowOffset = 25
# Set the height of the row where the cell is located
sheet.SetRowHeight(range.Row, 35)
# Set the width of the column where the cell is located
sheet.SetColumnWidth(range.Column, 17)
# Save the resulting file
workbook.SaveToFile("InsertOnlineImage.xlsx", FileFormat.Version2013)
workbook.Dispose()2. 嵌入图像:
- 使用 Python 的内置模块 urllib 从图像链接中读取图像数据。
- 使用 Spire.XLS 提供的 Worksheet.Pictures.AddPicture() 函数将图像数据添加到 Excel。
- 此方法将图像数据直接嵌入到 Excel 文件中。
- 由于数据存储在 Excel 文件中,因此无需任何外部下载即可显示图像。
下面的示例显示了如何通过嵌入图像将在线图像插入 Excel 中:
from spire.xls import *
from spire.xls.common import *
import urllib.request
# Create a Workbook object
workbook = Workbook()
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Specify the cell to add the image
range = sheet.Range["C2"]
# Specify the URL of the image you want to insert
image_url = "https://i.ibb.co/dJ8h49F/Medium.png"
# Use urllib to read the image data from the URL
with urllib.request.urlopen(image_url) as response:
image_data = response.read()
# Add the image to the worksheet
stream = Stream(image_data)
image = sheet.Pictures.Add(range.Row, range.Column, stream)
# Set the properties of the image to “move but don't size with cells”
image.ResizeBehave = ResizeBehaveType.MoveNotResize
# Set the left column and top row offsets for the image
image.LeftColumnOffset = 25
image.TopRowOffset = 25
# Set the height of the row where the cell is located
sheet.SetRowHeight(range.Row, 35)
# Set the width of the column where the cell is located
sheet.SetColumnWidth(range.Column, 17)
# Save the resulting file
workbook.SaveToFile("InsertOnlineImage.xlsx", FileFormat.Version2013)
workbook.Dispose()使用 Python 在 Excel 中压缩图像
当您需要减小包含大图像或高分辨率图像的 Excel 文件的大小时,在 Excel 中压缩图像非常有用。
要压缩 Excel 文件中的图像,您需要遍历每个 Excel 工作表中的图像,然后调用 ExcelPicture.Compress() 函数来压缩每个图像的质量。下面是代码示例:
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Images.xlsx")
# Iterate through the worksheets in the file
for sheet in workbook.Worksheets:
# Iterate through the images in the worksheet
for image in sheet.Pictures:
# Compress the image quality
image.Compress(50)
# Save the resulting file
workbook.SaveToFile("CompressImages.xlsx", FileFormat.Version2013)
workbook.Dispose()用 Python 替换 Excel 中的图像
有时您可能需要将 Excel 工作表中的旧图像更改为新图像。为此,您可以使用 ExcelPicture.Picture 属性。
下面的示例显示了如何将 Excel 工作表中的图像替换为另一个图像:
from spire.xls import *
from spire.xls.common import *
# Create a Workbook instance
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile ("Images.xlsx")
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Get the first image from the worksheet
excelPicture = sheet.Pictures[0]
# Specify the path to the new image
image_path = "logo.png"
# Replace the first image with the new image
stream = Stream(image_path)
excelPicture.Picture = stream
# Save the resulting file
workbook.SaveToFile("ReplaceImage.xlsx", ExcelVersion.Version2016)
workbook.Dispose()使用 Python 从 Excel 中提取图像
如果您需要将嵌入在 Excel 工作簿中的图像用于其他目的,例如在演示文稿或网站上,则提取过程是必不可少的。
要从 Excel 文件中提取图像,您可以使用 ExcelPicture.Picture.Save() 函数。下面是代码示例:
from spire.xls import *
from spire.xls.common import *
# Create a new Workbook instance
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Images.xlsx")
# Iterate through the worksheets in the workbook
for worksheet_index, worksheet in enumerate(workbook.Worksheets):
# Iterate through the images in the worksheet
for image_index, image in enumerate(worksheet.Pictures):
# Save each image as a PNG file
image_path = f"Images/Worksheet_{worksheet_index+1}_Image_{image_index+1}.png"
image.Picture.Save(image_path)
workbook.Dispose()使用 Python 从 Excel 中删除图像
当您想整理电子表格或删除不再需要的图像时,从 Excel 中删除图像非常有用。
您可以使用 Worksheet.Pictures[index] 从 Excel 工作表中删除一个或多个图像。Remove() 函数。下面是代码示例:
from spire.xls import *
from spire.xls.common import *
# Create a new Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Images.xlsx")
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Remove all images from the worksheet
for i in range(sheet.Pictures.Count - 1, -1, -1):
sheet.Pictures[i].Remove()
# Or remove a specific image by its index (0-based)
# sheet.Pictures[0].Remove()
# Save the resulting file
workbook.SaveToFile("RemoveImages.xlsx", FileFormat.Version2013)
workbook.Dispose()相关推荐
- u盘闪迪好还是金士顿好(金士顿和闪迪谁寿命长)
-
u盘金士顿好一些。金士顿的最大优点就是它的主控一般都是群联的,而此主控都有对应的量产工具,很多时候就算过了保质期,U盘不论出什么故障,只要硬件没坏就可以用量产工具来修复好继续用。而且就算某些金士顿主控...
- 深度ghost win10系统(deep ghost win10)
-
不能说绝对.要分两种情况:当win10安装时,如果采用了GPT分区硬盘格式(现在大多数新电脑都支持这个格式,并默认是这种方式),或者是由win10自动分区安装,均分产生一个额外的引导分区,容量比较少,...
- win8专业版和家庭版的区别(win8专业版和家庭版的区别在哪)
-
win10有七种版本:家庭版、企业版、教育版、移动版、移动企业版以及针对物联网设备及嵌入式系统设计的版本。据个人理解家庭版与家庭中文版是一个意思,是家庭版的不同语言版本,你升级的时候,会自动升级到对应...
- 手机开不了机怎么办一直黑屏
-
原因可能有很多:系统崩溃。刷机失败,手机电池用尽。可以通过以下方法检测:1.首先,对手机进行自我检测。现在大多数品牌手机都有深度休眠模式,一旦进入这种模式,经常会造成手机黑屏,无法开机。解决的...
- 主题商店官方下载(vivi主题商店下载)
-
1首先上小米官网注册设计师账号您可以登录主题设计师站上传。designer.xiaomi.com/2下载小米主题制作器3打包成mtz格式4上传等待审核(如果只是自己用的话就不用上传)在软件商店里搜...
- 360邮箱登录(360邮箱登录入口在哪里)
-
可以修改登录邮箱的,不自己亲自尝试一下,还真是觉的好麻烦,尤其是在你要修改的邮箱已经被注册的时候,如果你知道了,就很简单,不知道的,希望你再修改的时候不要多走弯路,下面我给大家详细讲解。1首先,我们要...
- win10自动更新后桌面文件全没了
-
打开控制面板,找到用户账户,看看有几个账户。如果有多个账户,则重新启动计算机,登录另一个账户,看看桌面文件是否回来了。以上方法未成功,则在整个电脑里搜索以前的那个文件夹。搜索也找不到,就下载并安装一个...
- 三星固态硬盘(三星固态硬盘序列号查询官网)
-
您可以通过访问三星官方网站的支持页面来查询三星固态硬盘的序列号。在支持页面中,您可以找到一个名为"产品注册"或"产品查询"的选项。点击该选项后,您将被要求输入您的固态硬...
- 手机恢复出厂设置后数据能恢复吗
-
1、首先来说如果点击了“恢复出厂设置”朋友们完全不用惊慌,因为手机上的数据还是能够找回来的。2、在网上找一款免费的手机恢复数据软件,例如安卓上的应用手机数据恢复精灵,根据手机恢复数据软件向导式提醒进行...
- 万能浏览器手机版下载安装(万能浏览器手机版下载安装最新版)
-
用起来还是挺靠谱的,但是可能会有捆绑的恶意软件,各种弹窗很烦人OPPO手机浏览器搜索网站的方法:在页面顶部的搜索栏输入URL或搜索关键字。搜索栏下会出现搜索建议,可直接点击符合你搜索目标的建议。点击搜...
- windows彻底关闭自动更新(关闭windows 自动更新)
-
方法一:Windows设置 要想关闭Win10自动更新,比较简单的一种方法就是进入到Windows设置中,将Windows更新直接关闭。步骤如下: 1、按“Windows+I”键,打开Wind...
- potplayer安卓版官网(potplayer apk下载)
-
教程如下:Potplayer是一款非常强大的媒体播放器,是由原KMPlayer的制作者自己开发的,软件体积小,功能强大,占用内存非常小,其软件内置解码器几乎能播放任何格式的媒体文件,而且软件本身没有任...
-
- 十大公认最耐用的台式电脑(哪个品牌的台式机电脑最耐用)
-
一般来说,品牌机没有单卖的,都是成套的主机显示器一起销售的,如果价格不是问题,单说耐用,个人感觉还是IBM的耐用,再其次什么戴尔,联想,华硕,宏碁什么的也都可以。惠普台式电脑和戴尔台式电脑相比较,肯定是戴尔台式电脑的质量比较好,因为戴尔台...
-
2025-12-13 13:03 off999
- 360下载的软件不在桌面上(电脑下载360为什么不在桌面)
-
方法如下。打开360浏览器的设置按钮,在设置中选择将下载的文件浏览到桌面,点击应用保存,这是即可将360文件下载的内容直接储存到桌面上。可以选择F3进行搜索,搜到360浏览器之后打开他的这个文件夹,找...
- 电脑读不了u盘怎么回事(电脑读不出u盘了)
-
方法1:取消勾选“隐藏的驱动器” 1、首先要排除是不是U盘损坏的问题,当U盘插入到其他电脑,如果可以读出来,那么肯定不是U盘的问题了。 2、很有可能是U盘在你的电脑上被隐藏了,将U盘插入电脑后,打...
欢迎 你 发表评论:
- 一周热门
-
-
抖音上好看的小姐姐,Python给你都下载了
-
全网最简单易懂!495页Python漫画教程,高清PDF版免费下载
-
Python 3.14 的 UUIDv6/v7/v8 上新,别再用 uuid4 () 啦!
-
飞牛NAS部署TVGate Docker项目,实现内网一键转发、代理、jx
-
python入门到脱坑 输入与输出—str()函数
-
宝塔面板如何添加免费waf防火墙?(宝塔面板开启https)
-
Python三目运算基础与进阶_python三目运算符判断三个变量
-
(新版)Python 分布式爬虫与 JS 逆向进阶实战吾爱分享
-
失业程序员复习python笔记——条件与循环
-
使用 python-fire 快速构建 CLI_如何搭建python项目架构
-
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (77)
- python封装 (57)
- python写入txt (66)
- python读取文件夹下所有文件 (59)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)
