Home >Backend Development >Python Tutorial >How to read a column in excel in python
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
<code>pip install openpyxl</code>
Step 2: Load the Excel file
<code class="python">import openpyxl wb = openpyxl.load_workbook('sample.xlsx')</code>
Step 3: Get the worksheet
<code class="python">sheet = wb['Sheet1']</code>
Step 4: Read a column
To read a column, You can specify the column number, such as column B:
<code class="python">column = sheet['B']</code>
Step 5: Iteratively read the cells in the column
<code class="python">for cell in column: print(cell.value)</code>
Sample code:
The following sample code shows how to read all cell values in column B in an Excel file:
<code class="python">import openpyxl wb = openpyxl.load_workbook('sample.xlsx') sheet = wb['Sheet1'] column = sheet['B'] for cell in column: print(cell.value)</code>
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!