使用 Python 在 Excel 中插入、压缩、替换、提取和删除图像
off999 2025-04-30 18:49 19 浏览 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()
相关推荐
- Python四种常用的高阶函数,你会用了吗
-
每天进步一点点,关注我们哦,每天分享测试技术文章本文章出自【码同学软件测试】码同学公众号:自动化软件测试码同学抖音号:小码哥聊软件测试1、什么是高阶函数把函数作为参数传入,这样的函数称为高阶函数例如:...
- Python之函数进阶-函数加强(上)(python函数的作用增强代码的可读性)
-
一.递归函数递归是一种编程技术,其中函数调用自身以解决问题。递归函数需要有一个或多个终止条件,以防止无限递归。递归可以用于解决许多问题,例如排序、搜索、解析语法等。递归的优点是代码简洁、易于理解,并...
- 数据分析-一元线性回归分析Python
-
前面几篇介绍了数据的相关性分析,通过相关性分析可以看出变量之间的相关性程度。如果我们已经发现变量之间存在明显的相关性了,接下来就可以通过回归分析,计算出具体的相关值,然后可以用于对其他数据的预测。本篇...
- python基础函数(python函数总结)
-
Python函数是代码复用的核心工具,掌握基础函数的使用是编程的关键。以下是Python函数的系统总结,包含内置函数和自定义函数的详细用法,以及实际应用场景。一、Python内置函数(...
- python进阶100集(9)int数据类型深入分析
-
一、基本概念int数据类型基本上来说这里指的都是整形,下一届我们会讲解整形和浮点型的转化,以及精度问题!a=100b=a这里a是变量名,100就是int数据对象,b指向的是a指向的对象,...
- Python学不会来打我(73)python常用的高阶函数汇总
-
python最常用的高阶函数有counter(),sorted(),map(),reduce(),filter()。很多高阶函数都是将一个基础函数作为第一个参数,将另外一个容器集合作为第二个参数,然...
- python中有哪些内置函数可用于编写数值表达式?
-
在Python中,用于编写数值表达式的内置函数很多,它们可以帮助你处理数学运算、类型转换、数值判断等。以下是常用的内置函数(不需要导入模块)按类别归类说明:一、基础数值处理函数函数作用示例ab...
- 如何在Python中获取数字的绝对值?
-
Python有两种获取数字绝对值的方法:内置abs()函数返回绝对值。math.fabs()函数还返回浮点绝对值。abs()函数获取绝对值内置abs()函数返回绝对值,要使用该函数,只需直接调用:a...
- 【Python大语言模型系列】使用dify云版本开发一个智能客服机器人
-
这是我的第359篇原创文章。一、引言上篇文章我们介绍了如何使用dify云版本开发一个简单的工作流:【Python大语言模型系列】一文教你使用dify云版本开发一个AI工作流(完整教程)这篇文章我们将引...
- Python3.11版本使用thriftpy2的问题
-
Python3.11于2022年10月24日发布,但目前thriftpy2在Python3.11版本下无法安装,如果有使用thriftpy2的童鞋,建议晚点再升级到最新版本。...
- uwsgi的python2+3多版本共存(python多版本兼容)
-
一、第一种方式(virtualenv)1、首先,机器需要有python2和python3的可执行环境。确保pip和pip3命令可用。原理就是在哪个环境下安装uwsgi。uwsgi启动的时候,就用的哪个...
- 解释一下Python脚本中版本号声明的作用
-
在Python脚本中声明版本号(如__version__变量)是一种常见的元数据管理实践,在IronPython的兼容性验证机制中具有重要作用。以下是版本号声明的核心作用及实现原理:一、版本号...
- 除了版本号声明,还有哪些元数据可以用于Python脚本的兼容性管理
-
在Python脚本的兼容性管理中,除了版本号声明外,还有多种元数据可以用于增强脚本与宿主环境的交互和验证。以下是一些关键的元数据类型及其应用场景:一、环境依赖声明1.Python版本要求pyth...
- 今年回家没票了?不,我有高科技抢票
-
零基础使用抢票开源软件Py12306一年一度的抢票季就要到了,今天给大家科普一下一款软件的使用方法。软件目前是开源的,禁止用于商用。首先需要在电脑上安装python3.7,首先从官网下载对应的安装包,...
- 生猛!春运抢票神器成GitHub热榜第一,过年回家全靠它了
-
作者:车栗子发自:凹非寺量子位报道春节抢票正在如火如荼的进行,过年回家那肯定需要抢票,每年的抢票大战,都是一场硬战,没有一个好工具,怎么能上战场死锁呢。今天小编推荐一个Python抢票工具,送到...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python字典遍历 (54)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (60)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)