Home >Backend Development >Python Tutorial >What to use in python to read excel

What to use in python to read excel

silencement
silencementOriginal
2019-06-20 14:48:492625browse

What to use in python to read excel

Hello everyone, let’s explore how to operate Excel files with Python 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.

What to use in python to read excel

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.

What to use in python to read excel

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.

What to use in python to read excel

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.

What to use in python to read excel

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.

What to use in python to read excel

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

What to use in python to read excel

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.

What to use in python to read excel

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.

What to use in python to read excel

The generated Excel file is as shown below:

What to use in python to read excel

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 What to use in python to read excel. 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