Home >Backend Development >Python Tutorial >How to read a column in excel in python

How to read a column in excel in python

下次还敢
下次还敢Original
2024-03-29 06:54:331129browse

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 a column in excel in python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn