Simple comparison of Python libraries that operate Excel

I was confused because there were multiple ways to play with Excel in Python, so I summarized them in a table.

Comparison table

Since the table is large, I posted Excel to another server without writing it in Qiita. Only the operations that are likely to be used often are described. http://mirutsurumi.html.xdomain.jp/Excel_pylib.html (Scheduled to be updated as appropriate.)

The following libraries are summarized.

use
openpyxl Treat Excel as Excel and use it for operations that care about cell formats. Click here for many explanations in books.
pandas Use this when you are not interested in Excel formats and want to specialize only in data analysis. openpyxl as engine variable,xlsxwriter can also be used.
xlwings (editing) What you can do is similar to openpyxl, but the operation feeling is different. When you execute it, Excel itself starts, and if you use an interactive shell, you can operate while watching the contents change. It can also officially read pandas df. Since you can also operate VBA, you can specify the cell range with range.("A1:C3")It is also easy to understand that you can specify like.
xlrd, xlwt Close to openpyxl, an image like its traditional version. Since it is also included in pandas, it seems unlikely that it will be used alone. You need to install these before you can operate Excel with pandas.
xlsxwriter Close to openpyxl, an image like its traditional version. Since it is also included in pandas, it seems unlikely that it will be used alone. You need to install xlsxwriter in advance when operating Excel with pandas.

The table is for simple reference, so detailed explanation is omitted.

Xlwings and openpyxl are also compared in the article below. https://qiita.com/m5knt/items/ab56f1d0a783f3422ee3

■ Basic procedure 【openpyxl, xlrd/xlwt, xlsxwriter】 ① Create wb object with library name.Workbook () ② ws = wb. Create ws object with sheet addition function (xlsxwriter is up to here) ③ In the same way, create from ws to cell object, and then operate with your favorite function. ④ Save (new Excel appears at this stage)

【pandas】 (1) Set data (read existing Excel with pd.read_excel) * You can also write directly with pd.DataFrame. (2) Cell value manipulation and filtering with df.loc etc. (Pivot_table can also be used although not shown in the table) ③ Create new Excel with df.to_excel

pandas supplement

Since the relationship between read_excel, to_excel, and ExcelWriter is complicated, I will supplement it.

■read_excel Read the data df of the existing Excel specified sheet (or the leftmost sheet if not specified). It is just reading df (DataFrame type) and does not define Excel workbook type.

■to_excel Write df to the specified Excel file.

■ExcelWriter Define the Excel file to be written with to_excel. (Similar to the image that defines wb in openpyxl.) You can write with to_excel (file name) without defining ExcelWriter, but by combining with with statement, you can write data to multiple sheets at once. In addition, the description of to_excel becomes redundant unless the Excel file name is made variable (in some cases, for each path) by combining this with the with statement. After all, it exists to simplify the description, not a mast to use.

Recommended Posts

Simple comparison of Python libraries that operate Excel
Operate Excel with Python (1)
Operate Excel with Python (2)
Python that merges a lot of excel into one excel
A quick comparison of Python and node.js test libraries
Operate Excel with Python openpyxl
Comparison of 4 Python web frameworks
Simple FPS measurement of python
Speed comparison of Python XML parsing
(Java, JavaScript, Python) Comparison of string processing
Comparison of Japanese conversion module in Python3
python string comparison / use'list'and'in' instead of'==' and'or'
Try to operate Excel using Python (Xlwings)
Comparison of Python serverless frameworks-Zappa vs Chalice
Comparison of matrix transpose speeds with Python
[Python] 3 types of libraries that improve log output a little [logzero, loguru, pyrogrus]
Convert "number" of excel date to python datetime
Performance comparison of face detector with Python + OpenCV
[Python3] Coarse graining of numpy.ndarray Speed comparison etc.
Automating simple tasks with Python Table of contents
Batch conversion of Excel files to JSON [Python]
Comparison of R and Python writing (Euclidean algorithm)
One-liner that outputs 10000 digits of pi with Python
Comparison of Python and Ruby (Environment / Grammar / Literal)
Summary of Excel operations using OpenPyXL in Python
Introduction of Python
First Python 3 ~ First comparison ~
Basics of Python ①
Basics of python ①
Copy of python
Excel with Python
Introduction of Python
Implementation example of simple LISP processing system (Python version)
[Python] A program that counts the number of valleys
Operate mongoDB from python in ubuntu environment ① Introduction of mongoDB
[Python] [Word] [python-docx] Simple analysis of diff data using python
List of Python libraries for data scientists and data engineers
Python implementation comparison of multi-index moving averages (DEMA, TEMA)
One liner that outputs 1000000 digits of pi in Python
Calculate the regression coefficient of simple regression analysis with python
Super simple: A collection of shells that output dates
Comparison table of frequently used processes of Python and Clojure
A collection of Excel operations often used in Python
The attitude that programmers should have (The Zen of Python)
[Python] A program that compares the positions of kangaroos.
Let's operate GPIO of Raspberry Pi with Python CGI
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
How to start a simple WEB server that can execute cgi of php and python
[Python] Creating a GUI tool that automatically processes CSV of temperature rise data in Excel