如何使用Openpyxl删除excel中的一行或多行?
Openpyxl是一个用于操作 xlsx/xlsm/xltx/xltm 文件的Python库。使用 Openpyxl,您可以创建新的 Excel 文件或工作表,也可以在现有的 Excel 文件或工作表上使用。
安装
这个模块没有内置于Python中。要安装此类型,请在终端中输入以下命令。
pip3 install openpyxl
在本文中,我们将讨论如何使用 openpyxl 删除 Excel 工作表中的行。您可以在此处找到本文使用的 Excel 文件。
删除空行(一个或多个)
方法一:
此方法删除空行但不继续空行,因为当您删除第一个空行时,下一行将获得其位置。所以没有经过验证。因此,这个问题可以通过递归函数调用来解决。
方法:
- 导入 openpyxl 库。
- 使用 openpyxl 加载 Excel 文件。
- 然后从文件加载工作表。
- 迭代加载的工作表中的行。
- 通过行删除函数。
- 然后检查每个单元格是否为空,如果任何非空单元格返回函数,那么只有空行才会退出 for 循环而不返回。
- 只有当所有行都为空时,才会执行 remove 语句。
- 最后,将文件保存到路径中。
Python3
# import openpyxl library
import openpyxl
# function to remove empty rows
def remove(sheet, row):
# iterate the row object
for cell in row:
# check the value of each cell in
# the row, if any of the value is not
# None return without removing the row
if cell.value != None:
return
# get the row number from the first cell
# and remove the row
sheet.delete_rows(row[0].row, 1)
if __name__ == '__main__':
# enter your file path
path = './delete_empty_rows.xlsx'
# load excel file
book = openpyxl.load_workbook(path)
# select the sheet
sheet = book['daily sales']
print("Maximum rows before removing:", sheet.max_row)
# iterate the sheet object
for row in sheet:
remove(sheet,row)
print("Maximum rows after removing:",sheet.max_row)
# save the file to the path
path = './openpy.xlsx'
book.save(path)
Python3
# import openpyxl library
import openpyxl
# function to remove empty rows
def remove(sheet):
# iterate the sheet by rows
for row in sheet.iter_rows():
# all() return False if all of the row value is None
if not all(cell.value for cell in row):
# detele the empty row
sheet.delete_rows(row[0].row, 1)
# recursively call the remove() with modified sheet data
remove(sheet)
return
if __name__ == '__main__':
# enter your file path
path = './delete_empty_rows.xlsx'
# load excel file
book = openpyxl.load_workbook(path)
# select the sheet
sheet = book['daily sales']
print("Maximum rows before removing:", sheet.max_row)
# iterate the sheet
for row in sheet:
remove(sheet)
print("Maximum rows after removing:",sheet.max_row)
# save the file to the path
path = './openpy.xlsx'
book.save(path)
Python3
import openpyxl
def delete(sheet):
# continuously delete row 2 untill there
# is only a single row left over
# that contains column names
while(sheet.max_row > 1):
# this method removes the row 2
sheet.delete_rows(2)
# return to main function
return
if __name__ == '__main__':
# enter your file path
path = './delete_every_rows.xlsx'
# load excel file
book = openpyxl.load_workbook(path)
# select the sheet
sheet = book['sheet1']
print("Maximum rows before removing:", sheet.max_row)
delete(sheet)
print("Maximum rows after removing:", sheet.max_row)
# save the file to the path
path = './openpy.xlsx'
book.save(path)
Python3
import openpyxl
if __name__ == '__main__':
# enter your file path
path = './delete_every_rows.xlsx'
# load excel file
book = openpyxl.load_workbook(path)
# select the sheet
sheet = book['sheet1']
print("Maximum rows before removing:", sheet.max_row)
# sheet.max_row is the maximum number
# of rows that the sheet have
# delete_row() method removes rows, first parameter represents row
# number and sencond parameter represents number of rows
# to delete from the row number
sheet.delete_rows(2, sheet.max_row-1)
print("Maximum rows after removing:", sheet.max_row)
# save the file to the path
path = './openpy.xlsx'
book.save(path)
输出:
Maximum rows before removing: 15
Maximum rows after removing: 14
删除后的文件:
第一种方法只删除第一个空行,不删除第二个连续的空行。
方法二:
此方法使用递归方法删除空行,包括连续的空行。关键是将修改后的工作表对象作为参数传递给递归函数。如果没有空行函数立即返回。
方法:
- 导入 openpyxl 库。
- 使用 openpyxl 加载 Excel 文件。
- 然后从文件加载工作表。
- 将加载的工作表传递给删除函数。
- 使用 iter_rows() 迭代行。
- 如果一行中的任何单元格不为空,则 any() 返回 false,因此立即返回。
- 如果一行中的所有单元格都为空,则使用 delete_rows() 删除该行。
- 然后将修改后的工作表对象传递给 remove函数,重复此操作直到到达工作表的末尾。
- 最后,将文件保存到路径中。
蟒蛇3
# import openpyxl library
import openpyxl
# function to remove empty rows
def remove(sheet):
# iterate the sheet by rows
for row in sheet.iter_rows():
# all() return False if all of the row value is None
if not all(cell.value for cell in row):
# detele the empty row
sheet.delete_rows(row[0].row, 1)
# recursively call the remove() with modified sheet data
remove(sheet)
return
if __name__ == '__main__':
# enter your file path
path = './delete_empty_rows.xlsx'
# load excel file
book = openpyxl.load_workbook(path)
# select the sheet
sheet = book['daily sales']
print("Maximum rows before removing:", sheet.max_row)
# iterate the sheet
for row in sheet:
remove(sheet)
print("Maximum rows after removing:",sheet.max_row)
# save the file to the path
path = './openpy.xlsx'
book.save(path)
输出:
Maximum rows before removing: 15
Maximum rows after removing: 13
删除后的文件:
此方法按预期删除了两个连续的空行。
删除所有行
方法一:
在这种方法中,我们重复删除第二行,直到只剩下一行(列名)。
方法:
- 导入 openpyxl 库。
- 加载要使用的 Excel 文件和工作表。
- 传递工作表对象删除函数。
- 删除第二行,直到只剩下一行。
- 最后,返回函数。
蟒蛇3
import openpyxl
def delete(sheet):
# continuously delete row 2 untill there
# is only a single row left over
# that contains column names
while(sheet.max_row > 1):
# this method removes the row 2
sheet.delete_rows(2)
# return to main function
return
if __name__ == '__main__':
# enter your file path
path = './delete_every_rows.xlsx'
# load excel file
book = openpyxl.load_workbook(path)
# select the sheet
sheet = book['sheet1']
print("Maximum rows before removing:", sheet.max_row)
delete(sheet)
print("Maximum rows after removing:", sheet.max_row)
# save the file to the path
path = './openpy.xlsx'
book.save(path)
输出:
Maximum rows before removing: 15
Maximum rows after removing: 1
删除后的文件:
方法二:
在这种方法中,我们使用 openpyxl sheet 方法通过单个命令删除整行。
方法:
- 导入 openpyxl 库。
- 加载要使用的 Excel 文件和工作表。
- 使用 delete_rows函数删除除列名之外的所有行。
- 所以只剩下一个空行。
蟒蛇3
import openpyxl
if __name__ == '__main__':
# enter your file path
path = './delete_every_rows.xlsx'
# load excel file
book = openpyxl.load_workbook(path)
# select the sheet
sheet = book['sheet1']
print("Maximum rows before removing:", sheet.max_row)
# sheet.max_row is the maximum number
# of rows that the sheet have
# delete_row() method removes rows, first parameter represents row
# number and sencond parameter represents number of rows
# to delete from the row number
sheet.delete_rows(2, sheet.max_row-1)
print("Maximum rows after removing:", sheet.max_row)
# save the file to the path
path = './openpy.xlsx'
book.save(path)
输出:
Maximum rows before removing: 15
Maximum rows after removing: 1
删除后的文件: