openpyxl库可以主动跳过空单元格而读取下方有数据的单元格,并将结果导入到新的Excel中。请为我写具体代码并写详细的中文注释,缩进请正确书写
时间: 2023-03-08 07:54:18 浏览: 153
from openpyxl import load_workbook
# 从Excel读取数据
wb = load_workbook('文件名.xlsx')
ws = wb.active
# 变量初始化
row_num = 0
# 创建一个新的Excel
wb_new = Workbook()
ws_new = wb_new.active
# 遍历每一行
for row in ws.iter_rows(min_row=1, max_row=ws.max_row):
cell_list = []
# 遍历每一列,跳过空单元格
for cell in row:
if cell.value is None:
continue
else:
cell_list.append(cell.value)
# 将有数据的单元格放入新的Excel中
ws_new.append(cell_list)
row_num = row_num + 1
# 保存新的Excel
wb_new.save('新文件名.xlsx')
阅读全文