我如何只能使用 openpyxl 打印列中的可见单元格(已过滤)

How I can only print the visible cells (filtered) in a column with openpyxl

我正在使用 excel sheet 并且只想打印在特定列中可见的单元格的值。下面是示例,我希望它打印为第二张图像,但它正在打印所有值 Excel_Example enter image description here

过滤如下 enter image description here

下面是我的代码

import openpyxl
import warnings

warnings.filterwarnings("ignore", category=DeprecationWarning)

excel_file_path_source = r'C:\Users\User\PycharmProjects\Test\demo.xlsx'
workbook_object = openpyxl.load_workbook(excel_file_path_source)
sheet_obj = workbook_object.get_sheet_by_name('Sheet1')

no_of_rows = sheet_obj.max_row
for i in range(2, no_of_rows+1):
    cell_obj = sheet_obj.cell(row=i, column=1)
    cell_obj_value = cell_obj.value
    if cell_obj_value is not None:
        print(cell_obj_value)

enter image description here

您可以使用下面的代码来实现您想要的....请注意,我已经给出了打印数据以及将过滤后的数据写入数据框的代码 df 以防万一您想使用数据进行进一步处理

from openpyxl import load_workbook

wb = load_workbook('input.xlsx') # use your workbook path
ws = wb['Sheet5'] # change to your excel sheet name
df = pd.DataFrame(columns = ['FnD','Date','Time'])

# iterate over all the rows in the sheet
for row in ws:
    # use if it has not been hidden
    if ws.row_dimensions[row[0].row].hidden == False:
        print(row[0].value, row[1].value, row[2].value) 
        if row[0].value != "FnD": #Ignore if header, else, write to DataFrame
            df.loc[len(df.index)] = [row[0].value, row[1].value, row[2].value] 

我未过滤的输入 Excel sheet

我的过滤输入 Excel sheet

输出

FnD Date Time
1121 2001-01-01 00:00:00 00:00:01
1122 2002-01-01 00:00:00 00:00:01
1123 2003-01-01 00:00:00 00:00:01
1124 2004-01-01 00:00:00 00:00:01

>>df
    FnD Date    Time
0   1121    2001-01-01  00:00:01
1   1122    2002-01-01  00:00:01
2   1123    2003-01-01  00:00:01
3   1124    2004-01-01  00:00:01