网站首页 > 技术文章 正文
Excel 电子表格通常包含需要定期更新的动态信息。您可以通过以编程方式自动执行此过程来简化工作流程,而不是手动搜索和修改每个出现的过时数据。
用于在 Excel 中查找和替换数据的 Python 库
要使用 Python 查找和替换 Excel 中的数据,我们可以使用 Spire.XLS for Python 库。
Spire.XLS for Python 是一个易于使用且功能丰富的库,用于在 Python 应用程序中创建、读取、编辑和转换 Excel 文件。使用此库,您可以处理许多电子表格格式,例如 XLS、XLSX、XLSB、XLSM 和 ODS。此外,您还可以将 Excel 文件渲染为其他类型的文件格式,例如 PDF、HTML、CSV、文本、图像、XML、SVG、ODS、PostScript 和 XPS。
您可以通过在终端中运行以下命令从 PyPI 安装 Spire.XLS for Python:
pip install Spire.Xls
有关安装的更多详细信息,您可以查看此官方文档:如何在 VS Code 中安装 Spire.XLS for Python。
使用 Python 查找和替换 Excel 工作表中的数据
Spire.XLS for Python 提供了 Worksheet.FindAll(findValue: str, flags: FindType, findOptions: ExcelFindOptions) 函数,该函数允许您在 Excel 工作表中查找包含特定数据的单元格。找到单元格后,您可以轻松地将其中包含的数据替换为所需的数据。
这是一个简单的示例,演示如何使用 Python 和 Spire.XLS for Python 在 Excel 工作表中查找和替换文本:
from spire.xls.common import *
from spire.xls import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Template.xlsx")
# Get the first worksheet
worksheet = workbook.Worksheets[0]
# Find the cells containing specific text within the worksheet
ranges = worksheet.FindAll("Components", FindType.Text, ExcelFindOptions.MatchEntireCellContent)
# Loop through the found cells
for range in ranges:
# Replace the text of each found cell with new text
range.Text = "Replacement Text"
# Set a highlight color for each found cell
range.Style.Color = Color.get_Yellow()
# Save the resulting file
workbook.SaveToFile("ReplaceDataInWorksheet.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
使用 Python 在 Excel 工作表中搜索和替换数据
使用 Python 在 Excel 中查找和替换特定行中的数据
您可以使用 Worksheet.Rows[index] 在特定行中查找包含特定数据的单元格。FindAll() 函数,然后将单元格的数据替换为新数据。
这是一个简单的示例,展示了如何使用 Python 和 Spire.XLS for Python 在 Excel 中查找和替换特定行中的文本:
from spire.xls.common import *
from spire.xls import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Template.xlsx")
# Get the first worksheet
worksheet = workbook.Worksheets[0]
# Find the cells containing a specific text within the second row
ranges = worksheet.Rows[1].FindAll("Components", FindType.Text, ExcelFindOptions.MatchEntireCellContent)
# Loop through the found cells
for range in ranges:
# Replace the text of each found cell with new text
range.Text = "Replacement Text"
# Set a highlight color for each found cell
range.Style.Color = Color.get_Yellow()
# Save the resulting file
workbook.SaveToFile("ReplaceDataInSpecificRow.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
使用 Python 在 Excel 中查找和替换特定列中的数据
同样,您可以通过使用 Worksheet.Columns[index] 来查找包含特定列中特定数据的单元格。FindAll() 函数,然后将单元格的数据替换为新数据。
下面是一个简单的示例,演示如何使用 Python 和 Spire.XLS for Python 在 Excel 中查找和替换特定列中的文本:
from spire.xls.common import *
from spire.xls import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Template.xlsx")
# Get the first worksheet
worksheet = workbook.Worksheets[0]
# Find the cells containing specific text within the third column
ranges = worksheet.Columns[2].FindAll("Socks", FindType.Text, ExcelFindOptions.MatchEntireCellContent)
# Loop through the found cells
for range in ranges:
# Replace the text of each found cell with new text
range.Text = "Replacement Text"
# Set a highlight color for each found cell
range.Style.Color = Color.get_Yellow()
# Save the resulting file
workbook.SaveToFile("ReplaceDataInSpecificColumn.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
使用 Python 在 Excel 中查找和替换特定单元格区域中的数据
除了搜索和替换特定工作表、行和列中的数据外,Spire.XLS for Python 还允许您搜索和替换指定单元格区域中的数据。
下面是一个简单的示例,演示如何使用 Python 和 Spire.XLS for Python 在 Excel 中查找和替换特定单元格区域中的文本:
from spire.xls.common import *
from spire.xls import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Template.xlsx")
# Get the first worksheet
worksheet = workbook.Worksheets[0]
# Find the cells containing specific text within the cell range "A2:D7"
ranges = worksheet.Range["A2:D7"].FindAll("Components", FindType.Text, ExcelFindOptions.MatchEntireCellContent)
# Loop through the found cells
for range in ranges:
# Replace the text of each found cell with new text
range.Text = "Replacement Text"
# Set a highlight color for each found cell
range.Style.Color = Color.get_Yellow()
# Save the resulting file
workbook.SaveToFile("ReplaceDataInCellRange.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
使用 Python 在 Excel 中查找和替换部分单元格数据
有时,您可能会遇到需要有选择地替换 Excel 单元格中特定部分或部分数据的情况。要完成此任务,您可以使用 FindAll() 和 TextPartReplace() 函数。
这是一个简单的示例,展示了如何使用 Python 和 Spire.XLS for Python 在 Excel 中查找和替换单元格文本的一部分:
from spire.xls.common import *
from spire.xls import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Template1.xlsx")
# Get the first worksheet
worksheet = workbook.Worksheets[0]
# Specify the text to find
keyword = "&=zsl"
# Find the cells containing the specified text within the worksheet
ranges = worksheet.FindAll(keyword, FindType.Text, ExcelFindOptions.none)
# Loop through the found cells
for range in ranges:
# Replace only the specified text with new data and keep the other text in the cell intact
range.TextPartReplace(keyword, "35")
# Save the resulting file
workbook.SaveToFile("ReplaceAPartOfCellText.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
- 上一篇: Python中如何使用二分法快速查找数据
- 下一篇: Python 常用算法:排序与查找
猜你喜欢
- 2025-03-12 Python大数据分析 遍历与条件判断结合的 8 大高频应用场景
- 2025-03-12 Python自动化办公高效批量查找Excel数据,轻松应对海量数据处理
- 2025-03-12 如何对日志文件进行二分查找?二分查找工具timecat介绍
- 2025-03-12 独家 | 5 个Python高级特性让你在不知不觉中成为Python高手
- 2025-03-12 pandas学习笔记-条件筛选(二)
- 2025-03-12 Python 杨氏矩形查找:高效编程新利器
- 2025-03-12 python字符串的查找
- 2025-03-12 python爬虫Selenium库详细教程
- 2025-03-12 Python高性能编程:五种核心优化技术的原理与Python代码
- 2025-03-12 Python查找众数及中位数
- 265℃Python短文,Python中的嵌套条件语句(六)
- 264℃python笔记:for循环嵌套。end=""的作用,图形打印
- 263℃PythonNet:实现Python与.Net代码相互调用!
- 259℃Python实现字符串小写转大写并写入文件
- 257℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 117℃原来2025是完美的平方年,一起探索六种平方的算吧
- 98℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 90℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
-
- 90%的Python程序员都不知道的(效率密码 )函数让代码直接封神!
- 【Python】性能加速之解析器加速Pypy 库使用说明
- 【Python】性能加速之解析器加速Brython 库使用说明
- Rust与Python的文件系统性能对比分析:你可能想知道的一切
- 比C语言还快20%!Mojo首个大模型开放下载,性能达Python版250倍
- Java、Go 和 Python 多线程性能对比
- Python编程通过懒属性提升性能(python性能调优)
- Python 3.13.0 重磅发布:性能起飞,GIL 不再是瓶颈!
- 精通Python多进程(Multiprocessing)提升性能:8 个进阶层次解析
- Python 3.13 启动自由线程,性能会下降吗?
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)