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

后端 未结 8 1161
-上瘾入骨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 18:51

    this is an alternative to previous answers in case you whish read one or more columns using openpyxl

    import openpyxl
    
    wb = openpyxl.load_workbook('origin.xlsx')
    first_sheet = wb.get_sheet_names()[0]
    worksheet = wb.get_sheet_by_name(first_sheet)
    
    #here you iterate over the rows in the specific column
    for row in range(2,worksheet.max_row+1):  
        for column in "ADEF":  #Here you can add or reduce the columns
            cell_name = "{}{}".format(column, row)
            worksheet[cell_name].value # the value of the specific cell
            ... your tasks... 
    

    I hope that this be useful.

提交回复
热议问题