如何使用Python替换 excel 中的单词?
Excel 是一个非常有用的工具,我们可以在其中获取行和列格式的数据。可以说,在数据库出现之前,excel就在数据的存储中发挥了重要作用。现在使用 Excel 输入,许多批处理正在完成。可能需要替换 Excel 表中的文本始终存在,因为 excel 始终保存重要数据。在本文中,让我们看看如何使用Python替换 Excel 中的单词
使用的方法和途径
下面让我们看看通过 xlwt & xlrd 包和 openpyxl 包,用于使用Python替换 excel 中的单词
方法一:xlwt & xlrd
要安装这些软件包,请在终端中键入以下命令。
# for writing into excel
pip install xlwt
# for reading
pip install xlrd
下面的代码有两个 excel 工作簿。一种用于阅读和获取文本。其他用于编写替换文本。我们可以做“n”次替换。该列应该完全具有需要替换的指定搜索文本,一旦找到它们就会被替换并写入新工作簿。
例子:
使用的 Excel 文件 –
Python3
import xlwt
import xlrd
# Excel file can be in your local drive
# and if not, specify the exact path
sampleWorkbook = xlrd.open_workbook('sampleexcel.xlsx')
originalSheet = sampleWorkbook.sheet_by_name('Test')
newWorkbookForTextReplacement = xlwt.Workbook()
newsheetForTextReplacement = newWorkbookForTextReplacement.add_sheet('Test')
replacementTextKeyPairs = {'Apple': 'Kiwi',
'Oranges': 'Lemons',
'Grapes': 'Papayas'}
# iterate over the rows of your sheet
# ncols - number of columns in the
# selected sheet, here it is for 'Test' sheet
# nrows - number of rows in the selected
# sheet, here it is for 'Test' sheet
for i in range(originalSheet.nrows):
print(i)
# Get the data of each column
data = [originalSheet.cell_value(i, col)
for col in range(originalSheet.ncols)]
for index, value in enumerate(data):
# If any key present in replacementTextKeyPairs
# matches with excel column value, replace the
# column with the value
if value in replacementTextKeyPairs.keys():
newsheetForTextReplacement.write(
i, index, str(replacementTextKeyPairs.get(value)))
else:
newsheetForTextReplacement.write(i, index, value)
# Replaced text will be present in the new workbook
# with name sampleexcelwithreplacedtext.xls
newWorkbookForTextReplacement.save('sampleexcelwithreplacedtext.xls')
Python3
# Reading and writing in excel can be done by single module
import openpyxl
from openpyxl.utils.cell import get_column_letter
workbook = openpyxl.load_workbook('sampleexcelopenpyxl.xlsx')
workbook.sheetnames
worksheet = workbook["Test"]
# Number of rows
number_of_rows = worksheet.max_row
# Number of columns
number_of_columns = worksheet.max_column
replacementTextKeyPairs = {'1': 'One', '2': 'Two', '3': 'Three'}
# Iterate over the columns and rows, search
# for the text and replace
for i in range(number_of_columns):
for k in range(number_of_rows):
cellValue = str(worksheet[get_column_letter(i+1)+str(k+1)].value)
for key in replacementTextKeyPairs.keys():
if str(cellValue) == key:
newCellValue = replacementTextKeyPairs.get(key)
worksheet[get_column_letter(i+1)+str(k+1)] = str(newCellValue)
workbook.save('sampleexcelwithreplacedtextusingopenpyxl.xlsx')
输出:
方法2:openpyxl
要安装此模块,请在终端中键入以下命令。
pip install openpyxl
openpyxl 包的优点是它可以用于读取和写入 xlsx/xlsm/xltx/xltm 文件。下面的代码使用 openpyxl 从一个 excel 文件中读取和获取文本,替换文本并写入另一个 excel 文件。
例子:
使用的 Excel 文件 –
Python3
# Reading and writing in excel can be done by single module
import openpyxl
from openpyxl.utils.cell import get_column_letter
workbook = openpyxl.load_workbook('sampleexcelopenpyxl.xlsx')
workbook.sheetnames
worksheet = workbook["Test"]
# Number of rows
number_of_rows = worksheet.max_row
# Number of columns
number_of_columns = worksheet.max_column
replacementTextKeyPairs = {'1': 'One', '2': 'Two', '3': 'Three'}
# Iterate over the columns and rows, search
# for the text and replace
for i in range(number_of_columns):
for k in range(number_of_rows):
cellValue = str(worksheet[get_column_letter(i+1)+str(k+1)].value)
for key in replacementTextKeyPairs.keys():
if str(cellValue) == key:
newCellValue = replacementTextKeyPairs.get(key)
worksheet[get_column_letter(i+1)+str(k+1)] = str(newCellValue)
workbook.save('sampleexcelwithreplacedtextusingopenpyxl.xlsx')
输出:
注意: openpyxl 不支持旧的 .xls 文件格式,使用 xlrd 读取这个文件,或者将其转换为更新的 .xlsx 文件格式