
Today many professionals—data scientists, analysts, and automation engineers—use Python to work with Excel (XLS, XLSX) files. Python libraries automate data manipulation, eliminating manual Excel work. In this article you will learn how to create and read Excel files in Python with a Python Excel library, and how to add data, pivot tables, and charts.
This article will cover the following points:
- Installing Python Excel Library
- How to Read Excel (XLS or XLSX) Files in Python
- Create Excel Files in Python
- Writing to an Excel Sheet using Python
- Creating Charts in Excel Sheets in Python
- Adding Pivot Table in Excel using Python
- FAQs
Python Excel Library - Installation
To create and read Excel (XLS, XLSX) files in Python, we will use Aspose.Cells for Python via Java. The efficacy of this Python Excel library is manifest in its ability to cater to a litany of tasks related to Excel files, such as data extraction, data manipulation, and even the creation of Excel files from scratch.
Please download or install the package from PyPI using the pip command given below:
pip install aspose-cells
Reading Excel Files in Python
An Excel workbook contains worksheets, each with a grid of cells identified by row and column indexes. While many packages can read Excel files in Python, this section uses Aspose Python Excel library.
Following are the steps to read XLSX in Python:
- First, load the Excel file using the Workbook class.
- Then, obtain a reference to the WorksheetCollection using Workbook.getWorksheets() method.
- Finally, loop through the worksheets in the collection, and for each worksheet, iterate through its rows and columns to access and print the cell values.
The following code sample demonstrates how to read an Excel file using Python.
To reference a specific worksheet, use the Workbook.getWorksheets().get(index) method. The following code sample shows such a case.
Python Create Excel Files
This section shows how to create Excel files in Python.
Following are the steps to create an Excel file in Python:
- Firstly, create a new object of Workbook class.
- Secondly, get the reference of the desired Worksheet using Workbook.getWorksheets().get(index) method.
- After that, insert values in the desired cells using Worksheet.getCells().get().putValue() method.
- Lastly, save the workbook using Workbook.Save method.
The following code sample demonstrates how to create an Excel file using Python.
You will see the output as follows:
Python Write Data to Excel
We have covered how to create and read Excel files in Python with our Python Excel library. Next, let’s write data to an Excel file. Create or load a workbook as shown earlier, then use the Worksheet.getCells().get().putValue() method to write values to specific cells.
Python code for writing data to Excel file is given below:
The output will be shown as follows:
Python Creating Charts in Excel Sheets
Charts visualize data trends and relationships. Our Python Excel library supports many chart types—bar, column, line, scatter, pie, and more—that can be customized. Below is how to create a chart in an Excel sheet.
The steps to create a chart in an Excel file in Python are given as follows:
- Create or load an Excel file using the Workbook class.
- Add values in the cells of the desired worksheet.
- Obtain chart collection using the Worksheet.getCharts() method.
- Add a new chart in the charts collection using Worksheet.getCharts().add(type, upperLeftRow, upperLeftColumn, lowerRightRow, lowerRightColumn) method.
- Specify NSeries for the chart.
- Save the Excel file using the Workbook.save(fileName) method.
The following code shows how to create a chart in an Excel sheet using Python:
The following screenshot shows the output of the above code.
Python Excel Pivot Table: How to Create Pivot Table in Excel using Python
Pivot tables let you quickly analyze large data sets by summarizing and organizing information. This section shows how to create a pivot table in Excel using Python.
Steps to insert pivot tables in Excel in Python are given below:
- Initially, load or create a new Excel file using the Workbook class.
- Then, add desired data to the worksheet.
- After that, Insert a new pivot table using Worksheet.getPivotTables().add(sourceData, destCellName, tableName) method.
- Then, Specify the row, column, and data areas of the pivot table.
- Finally, Save the workbook.
The following code demonstrates how to create a pivot table in an Excel sheet using Python:
The following image displays the output of the above code.
Python Excel Library - FAQs
What is Aspose.Cells for Python via Java?
Aspose.Cells for Python via Java is a Python Excel Library for working with Excel files in Python. It includes methods for generating, altering, and formatting Excel files, as well as reading and writing data to and from Excel files.
How do I install Aspose.Cells for Python via Java?
You can install Aspose.Cells for Python via Java using pip, the package installer for Python. Open your command prompt or terminal and enter the following command:
pip install aspose-cells.
How do I create an Excel file using Python?
I have explained it in the section Create Excel Files in Python of this blog post.
How do I read an Excel file using Python?
This topic has been discussed in the section Read Excel Files in Python of this blog post.
Get a Free License
You can download a free temporary license to test the API without evaluation limitations.
Summing Up
We are ending this article here, we hope you have learned how to create and read Excel files in Python. We have briefly explained both cases. Moreover, you have seen how to add pivot tables, charts, and simple data in Excel using our powerful Python Excel manipulation library.
To learn more about the Python Excel library, you may visit the documentation.
Lastly, please keep visiting conholdate.com for regular updates.
Ask a Question
You can post your questions or comments in our forum.