Openpyxl - How to read only one column from Excel file in Python?

后端 未结 8 1172
-上瘾入骨i
-上瘾入骨i 2020-12-14 18:14

I want to pull only column A from my spreadsheet. I have the below code, but it pulls from all columns.

from openpyxl import Workbook, load_workbook

wb=load         


        
8条回答
  •  眼角桃花
    2020-12-14 19:02

    Here is a simple function:

    import openpyxl
    
    def return_column_from_excel(file_name, sheet_name, column_num, first_data_row=1):
        wb = openpyxl.load_workbook(filename=file_name)
        ws = wb.get_sheet_by_name(sheet_name)
        min_col, min_row, max_col, max_row = (column_num, first_data_row, column_num, ws.max_row)
        return ws.get_squared_range(min_col, min_row, max_col, max_row)
    

提交回复
热议问题