如何使用Openpyxl筛选列数据

2022-04-07 00:00:00 python openpyxl

问题描述

我正在尝试对现有的Excel文件应用筛选器,并将其导出到另一个Excel文件。我想提取只包含值16的行,然后将表导出到另一个Excel文件(如下图所示)。

我已经多次尝试阅读Openpyxl文档并搜索解决方案,但我仍然无法使我的代码工作。我还附上了下面的代码和文件

import openpyxl
# Is use to create a reference of the Excel to wb
 wb1 = openpyxl.load_workbook('test_data.xlsx')
 wb2 = openpyxl.load_workbook('test_data_2.xlsx')

# Refrence the workbook to the worksheets
 sh1 = wb1["data_set_1"]
 sh2 = wb2["Sheet1"]

 sh1.auto_filter.ref = "A:A"
 sh1.auto_filter.add_filter_column(0, ["16"])
 sh1.auto_filter.add_sort_condition("B2:D6")

 sh1_row_number = sh1.max_row
 sh1_col_number = sh1.max_column

 rangeSelected = []
 for i in range(1, sh1_row_number+1, 1):
     rowSelected = []
     for j in range(1, sh1_col_number+1, 1):
         rowSelected.append(sh1.cell(row = i, column = j))
     rangeSelected.append(rowSelected)

  del rowSelected

 for i in range(1, sh1_row_number+1, 1):
    for j in range(1, sh1_col_number+1, 1):
        sh2.cell(row = i, column = j).value = rangeSelected[i-1][j-1].value

 wb1.save("test_data.xlsx")
 wb2.save("test_data_2.xlsx")

The pictures shows what should be the desire result


解决方案

自动筛选实际上并不筛选数据,它只是为了可视化。 您可能希望在循环访问工作簿时进行筛选。请注意,对于这段代码,我假设您在第二个工作簿中已经有了表头。它不会覆盖数据,而是追加到表中。

import openpyxl
# Is use to create a reference of the Excel to wb
wb1 = openpyxl.load_workbook('test_data.xlsx')
wb2 = openpyxl.load_workbook('test_data_2.xlsx')

# Refrence the workbook to the worksheets
sh1 = wb1["data_set_1"]
sh2 = wb2["data_set_1"]   # use same sheet name, different workbook

for row in sh1.iter_rows():
    if row[0].value == 16:   # filter on first column with value 16
        sh2.append((cell.value for cell in row))     

wb1.save("test_data.xlsx")
wb2.save("test_data_2.xlsx")

相关文章