Home Backend Development Python Tutorial How to read excel table in python

How to read excel table in python

Jun 22, 2019 pm 02:03 PM

How to read excel table in python

Hello everyone, let’s explore how to use Python to operate Excel files today. Similar to the word file operation library python-docx, Python also has special libraries to provide support for Excel file operations. These libraries include xlrd, xlwt, xlutils, openpyxl, and xlsxwriter. Among them, my favorite is openpyxl, which is also The main content of this explanation. Excel files are familiar to everyone. They are used in daily work and study. Let's recall, what are the steps for you to operate an Excel file? The picture below shows an Excel file. Let’s compare it and think about it.

How to read excel table in python

OK, let’s walk through it together. First, we need to create or open an Excel file, and then select an A worksheet, which is the sheet in the picture above, finally reads or sets the value of cell. Correspondingly, in openpyxl, there are three concepts: Workbooks, Sheets, and Cells. Workbook is an open excel file, that is, an excel workbook; Sheet is a table in the workbook, that is, a worksheet; Cell is a simple cell. openpyxl revolves around these three concepts. Regardless of reading and writing, it is "three things": open the Workbook, locate the Sheet, and operate the Cell. OK, now that we understand the basic concepts, let’s see it in action!

First of all, openpyxl is not a pre-installed library of Python 3. We need to install it manually. It is very simple to open the command line window and enter pip install openpyxl. As shown in the picture below, mine has been installed, so the output information may be different from everyone else's.

How to read excel table in python

After installing openpyxl, import it through the import statement, and then execute the help method to see To see what is included in the openpyxl library, you don’t need to know it, just have an impression.

How to read excel table in python

Some words in it are still very familiar, such as cell, chart, styles, workbook, worksheet, In addition to using the help method, you can also use the dir method to view all members of a library. I have marked some that we may use later in red. You can focus on them during the learning process.

How to read excel table in python

The following are the general steps for operating Excel files:

1. Open or create an Excel : You need to create a workbook object. The load_workbook method is used to open an Excel, and creating an Excel is done directly by instantiating the workbook class.

2. Get a worksheet: You need to create a workbook object first, and then use the method of the object to get a worksheet object.

3. If you want to get the data in the table, you need to get a worksheet object first, and then get the Cell object representing the cell from it.

OK, let’s take a look at the actual operation in Python. The object of the operation is the position list of civil servants entered in the Hainan Examination in 2018, as shown in the figure below.

How to read excel table in python

Some basic operation examples are given below, you can follow them and write them down.

How to read excel table in python

Show the operation again, read the cells in the specified row and row at once, use the iter_rows method, which means iterate by row within the range specified by the parameter , if you want to iterate by columns, you can use the iter_cols method.

How to read excel table in python

The above code shows how to operate an existing Excel file. Let’s take a look at the example of creating a new Excel file.

How to read excel table in python

The generated Excel file is as shown below:

How to read excel table in python

OK, do you feel that operating Excel is very easy? That’s because you have made progress in learning Python during this period. Give yourself a thumbs up! Thank you for your attention and reading. There will be more delicious programming in the future, so please enjoy it.

The above is the detailed content of How to read excel table in python. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

See all articles