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

How to read a column in excel in python

下次还敢
Release: 2024-03-29 06:54:33
Original
1147 people have browsed it

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>
Copy after login

Step 2: Load the Excel file

<code class="python">import openpyxl

wb = openpyxl.load_workbook('sample.xlsx')</code>
Copy after login

Step 3: Get the worksheet

<code class="python">sheet = wb['Sheet1']</code>
Copy after login

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>
Copy after login

Step 5: Iteratively read the cells in the column

<code class="python">for cell in column:
    print(cell.value)</code>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template