How to read a column in excel in python

下次还敢
Release: 2024-03-29 06:54:33
Original
878 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

pip install openpyxl
Copy after login

Step 2: Load the Excel file

import openpyxl wb = openpyxl.load_workbook('sample.xlsx')
Copy after login

Step 3: Get the worksheet

sheet = wb['Sheet1']
Copy after login

Step 4: Read a column

To read a column, You can specify the column number, such as column B:

column = sheet['B']
Copy after login

Step 5: Iteratively read the cells in the column

for cell in column: print(cell.value)
Copy after login

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)
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 Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!