How to read an Excel column in Python: install the openpyxl library. Load the Excel file. Get the worksheet. Specify the column number and get the column. Iteratively reads the cell values in a column.
How to read an Excel column in Python
In Python, you can use the third-party library openpyxl to read Get the Excel file. To read a column, you can take the following steps:
Step 1: Install the openpyxl library
pip install openpyxl
Step 2: Load the Excel file
import openpyxl wb = openpyxl.load_workbook('sample.xlsx')
Step 3: Get the worksheet
sheet = wb['Sheet1']
Step 4: Read a column
To read a column, You can specify the column number, such as column B:
column = sheet['B']
Step 5: Iteratively read the cells in the column
for cell in column: print(cell.value)
Sample code:
The following sample code shows how to read all cell values in column B in an Excel file:
import openpyxl wb = openpyxl.load_workbook('sample.xlsx') sheet = wb['Sheet1'] column = sheet['B'] for cell in column: print(cell.value)
The above is the detailed content of How to read a column in excel in python. For more information, please follow other related articles on the PHP Chinese website!