User Guide Tutorials Python tutorials Tutorial 25: How to create pivot table in Python How to create pivot table in Python
EasyXLS Excel library can be used to export Excel files with Python on Windows, Linux, Mac or other operating systems. The integration vary depending on the operating system or if the bridge for .NET Framework of Java is chosen:
EasyXLS on Windows using .NET Framework with Python If you opt for the .NET version of EasyXLS, the below code requires Pythonnet , a bridge between Python and .NET Framework.
Step 1: Download EasyXLS Excel Library for .NET To download the trial version of EasyXLS Excel Library, press the below button:
If you already own a license key, you may login and download EasyXLS from your account. Install the downloaded EasyXLS installer for v8.6 or earlier.
Step 2: Install Pythonnet For the installation you need to run "pip" command as it follows. Pip is a package-management system used to install and manage software packages written in Python. <Python installation path>\Scripts>pip install "pythonnet.whl"
Step 3: Include EasyXLS library into project EasyXLS.dll must be added to your project. EasyXLS.dll can be found: - Inside the downloaded archive at Step 1 for EasyXLS v9.0 or later - Under installation path for EasyXLS v8.6 or earlier, in "Dot NET version" folder.
Step 4: Run Python code that creates a pivot table in Excel file Execute the following Python code that exports a pivot table to Excel.
"""------------------------------------------------------
Tutorial 25
This tutorial shows how to create an Excel file in Python
and to create a pivot table in a worksheet.
------------------------------------------------------"""
import clr
import gc
clr.AddReference('EasyXLS' )
from EasyXLS import *
from EasyXLS.PivotTables import *
from EasyXLS.Constants import *
print("Tutorial 25\n----------\n" )
workbook = ExcelDocument(2)
workbook.easy_getSheetAt(0).setSheetName("First tab" )
workbook.easy_getSheetAt(1).setSheetName("Second tab" )
xlsFirstTable = workbook.easy_getSheetAt(0).easy_getExcelTable()
xlsFirstTable.easy_getCell(0,0).setValue("Sale agent" )
xlsFirstTable.easy_getCell(0,0).setDataType(DataType.STRING)
xlsFirstTable.easy_getCell(0,1).setValue("Sale country" )
xlsFirstTable.easy_getCell(0,1).setDataType(DataType.STRING)
xlsFirstTable.easy_getCell(0,2).setValue("Month" )
xlsFirstTable.easy_getCell(0,2).setDataType(DataType.STRING)
xlsFirstTable.easy_getCell(0,3).setValue("Year" )
xlsFirstTable.easy_getCell(0,3).setDataType(DataType.STRING)
xlsFirstTable.easy_getCell(0,4).setValue("Sale amount" )
xlsFirstTable.easy_getCell(0,4).setDataType(DataType.STRING)
xlsFirstTable.easy_getRowAt(0).setBold(True )
xlsFirstTable.easy_getCell(1,0).setValue("John Down" )
xlsFirstTable.easy_getCell(1,1).setValue("USA" )
xlsFirstTable.easy_getCell(1,2).setValue("June" )
xlsFirstTable.easy_getCell(1,3).setValue("2010" )
xlsFirstTable.easy_getCell(1,4).setValue("550" )
xlsFirstTable.easy_getCell(2,0).setValue("Scott Valey" )
xlsFirstTable.easy_getCell(2,1).setValue("United Kingdom" )
xlsFirstTable.easy_getCell(2,2).setValue("June" )
xlsFirstTable.easy_getCell(2,3).setValue("2010" )
xlsFirstTable.easy_getCell(2,4).setValue("2300" )
xlsFirstTable.easy_getCell(3,0).setValue("John Down" )
xlsFirstTable.easy_getCell(3,1).setValue("USA" )
xlsFirstTable.easy_getCell(3,2).setValue("July" )
xlsFirstTable.easy_getCell(3,3).setValue("2010" )
xlsFirstTable.easy_getCell(3,4).setValue("3100" )
xlsFirstTable.easy_getCell(4,0).setValue("John Down" )
xlsFirstTable.easy_getCell(4,1).setValue("USA" )
xlsFirstTable.easy_getCell(4,2).setValue("June" )
xlsFirstTable.easy_getCell(4,3).setValue("2011" )
xlsFirstTable.easy_getCell(4,4).setValue("1050" )
xlsFirstTable.easy_getCell(5,0).setValue("John Down" )
xlsFirstTable.easy_getCell(5,1).setValue("USA" )
xlsFirstTable.easy_getCell(5,2).setValue("July" )
xlsFirstTable.easy_getCell(5,3).setValue("2011" )
xlsFirstTable.easy_getCell(5,4).setValue("2400" )
xlsFirstTable.easy_getCell(6,0).setValue("Steve Marlowe" )
xlsFirstTable.easy_getCell(6,1).setValue("France" )
xlsFirstTable.easy_getCell(6,2).setValue("June" )
xlsFirstTable.easy_getCell(6,3).setValue("2011" )
xlsFirstTable.easy_getCell(6,4).setValue("1200" )
xlsFirstTable.easy_getCell(7,0).setValue("Scott Valey" )
xlsFirstTable.easy_getCell(7,1).setValue("United Kingdom" )
xlsFirstTable.easy_getCell(7,2).setValue("June" )
xlsFirstTable.easy_getCell(7,3).setValue("2011" )
xlsFirstTable.easy_getCell(7,4).setValue("700" )
xlsFirstTable.easy_getCell(8,0).setValue("Scott Valey" )
xlsFirstTable.easy_getCell(8,1).setValue("United Kingdom" )
xlsFirstTable.easy_getCell(8,2).setValue("July" )
xlsFirstTable.easy_getCell(8,3).setValue("2011" )
xlsFirstTable.easy_getCell(8,4).setValue("360" )
xlsPivotTable = ExcelPivotTable()
xlsPivotTable.setName("Sales" )
xlsPivotTable.setSourceRange("First tab!$A$1:$E$9" , workbook)
xlsPivotTable.setLocation("A3:G15" )
xlsPivotTable.addFieldToRowLabels("Sale agent" )
xlsPivotTable.addFieldToColumnLabels("Year" )
xlsPivotTable.addFieldToValues("Sale amount" ,"Sale amount per year" , PivotTable.SUBTOTAL_SUM)
xlsPivotTable.addFieldToReportFilter("Sale country" )
xlsPivotTable.setOutlineForm()
xlsPivotTable.setStyle(PivotTable.PIVOT_STYLE_DARK_11)
workbook.easy_getSheet("Second tab" ).easy_addPivotTable(xlsPivotTable)
print("Writing file C:\\Samples\\Tutorial25 - pivot table in Excel.xlsx." )
workbook.easy_WriteXLSXFile("C:\\Samples\\Tutorial25 - pivot table in Excel.xlsx" )
sError = workbook.easy_getError()
if sError == "" :
print("\nFile successfully created.\n\n" )
else:
print("\nError encountered: " + sError + "\n\n" )
gc.collect()
EasyXLS on Linux, Mac, Windows using Java with Python If you opt for the Java version of EasyXLS, a similar code as above requires Py4J , Pyjnius or any other bridge between Python and Java.
Step 1: Download EasyXLS Excel Library for Java To download the trial version of EasyXLS Excel Library, press the below button:
If you already own a license key, you may login and download EasyXLS from your account. Install the downloaded EasyXLS installer for v8.6 or earlier.
Step 2: Install Py4j For the Py4j installation you need to run "pip" command as it follows. Pip is a package-management system used to install and manage software packages written in Python. <Python installation path>\Scripts>pip install "py4j.whl"
Step 3: Create additional Java program The following Java code needs to be running in the background prior to executing the Python code.
import py4j.GatewayServer;
public class GatewayServerApp {
public static void main(String[] args) {
GatewayServerApp app = new GatewayServerApp();
GatewayServer server = new GatewayServer(app);
server.start();
}
}
Step 4: Add py4j library to CLASSPATH py4j.jar must be added to your classpath of the additional Java program. py4j.jar can be found after installing Py4j, in "<Python installation path>\share\py4j" folder.
Step 5: Add EasyXLS library to CLASSPATH EasyXLS.jar must be added to your classpath of the additional Java program. EasyXLS.jar can be found: - Inside the downloaded archive at Step 1 for EasyXLS v9.0 or later - Under installation path for EasyXLS v8.6 or earlier, in "Lib" folder.
Step 6: Run additional Java program Start the gateway server application and it will implicitly start Java Virtual Machine as well.
Step 7: Run Python code that creates a pivot table in Excel file Execute a code as below Python code that exports a pivot table to Excel.
"""----------------------------------------------------------
Tutorial 25
This tutorial shows how to create an Excel file in Python and
to create a pivot table in a worksheet.
----------------------------------------------------------"""
import gc
from py4j.java_gateway import JavaGateway
from py4j.java_gateway import java_import
gateway = JavaGateway()
java_import(gateway.jvm,'EasyXLS.*' )
java_import(gateway.jvm,'EasyXLS.Constants.*' )
java_import(gateway.jvm,'EasyXLS.PivotTables.*' )
print("Tutorial 25\n----------\n" )
workbook = gateway.jvm.ExcelDocument(2)
workbook.easy_getSheetAt(0).setSheetName("First tab" )
workbook.easy_getSheetAt(1).setSheetName("Second tab" )
xlsFirstTable = workbook.easy_getSheetAt(0).easy_getExcelTable()
xlsFirstTable.easy_getCell(0,0).setValue("Sale agent" )
xlsFirstTable.easy_getCell(0,0).setDataType(gateway.jvm.DataType.STRING)
xlsFirstTable.easy_getCell(0,1).setValue("Sale country" )
xlsFirstTable.easy_getCell(0,1).setDataType(gateway.jvm.DataType.STRING)
xlsFirstTable.easy_getCell(0,2).setValue("Month" )
xlsFirstTable.easy_getCell(0,2).setDataType(gateway.jvm.DataType.STRING)
xlsFirstTable.easy_getCell(0,3).setValue("Year" )
xlsFirstTable.easy_getCell(0,3).setDataType(gateway.jvm.DataType.STRING)
xlsFirstTable.easy_getCell(0,4).setValue("Sale amount" )
xlsFirstTable.easy_getCell(0,4).setDataType(gateway.jvm.DataType.STRING)
xlsFirstTable.easy_getRowAt(0).setBold(True )
xlsFirstTable.easy_getCell(1,0).setValue("John Down" )
xlsFirstTable.easy_getCell(1,1).setValue("USA" )
xlsFirstTable.easy_getCell(1,2).setValue("June" )
xlsFirstTable.easy_getCell(1,3).setValue("2010" )
xlsFirstTable.easy_getCell(1,4).setValue("550" )
xlsFirstTable.easy_getCell(2,0).setValue("Scott Valey" )
xlsFirstTable.easy_getCell(2,1).setValue("United Kingdom" )
xlsFirstTable.easy_getCell(2,2).setValue("June" )
xlsFirstTable.easy_getCell(2,3).setValue("2010" )
xlsFirstTable.easy_getCell(2,4).setValue("2300" )
xlsFirstTable.easy_getCell(3,0).setValue("John Down" )
xlsFirstTable.easy_getCell(3,1).setValue("USA" )
xlsFirstTable.easy_getCell(3,2).setValue("July" )
xlsFirstTable.easy_getCell(3,3).setValue("2010" )
xlsFirstTable.easy_getCell(3,4).setValue("3100" )
xlsFirstTable.easy_getCell(4,0).setValue("John Down" )
xlsFirstTable.easy_getCell(4,1).setValue("USA" )
xlsFirstTable.easy_getCell(4,2).setValue("June" )
xlsFirstTable.easy_getCell(4,3).setValue("2011" )
xlsFirstTable.easy_getCell(4,4).setValue("1050" )
xlsFirstTable.easy_getCell(5,0).setValue("John Down" )
xlsFirstTable.easy_getCell(5,1).setValue("USA" )
xlsFirstTable.easy_getCell(5,2).setValue("July" )
xlsFirstTable.easy_getCell(5,3).setValue("2011" )
xlsFirstTable.easy_getCell(5,4).setValue("2400" )
xlsFirstTable.easy_getCell(6,0).setValue("Steve Marlowe" )
xlsFirstTable.easy_getCell(6,1).setValue("France" )
xlsFirstTable.easy_getCell(6,2).setValue("June" )
xlsFirstTable.easy_getCell(6,3).setValue("2011" )
xlsFirstTable.easy_getCell(6,4).setValue("1200" )
xlsFirstTable.easy_getCell(7,0).setValue("Scott Valey" )
xlsFirstTable.easy_getCell(7,1).setValue("United Kingdom" )
xlsFirstTable.easy_getCell(7,2).setValue("June" )
xlsFirstTable.easy_getCell(7,3).setValue("2011" )
xlsFirstTable.easy_getCell(7,4).setValue("700" )
xlsFirstTable.easy_getCell(8,0).setValue("Scott Valey" )
xlsFirstTable.easy_getCell(8,1).setValue("United Kingdom" )
xlsFirstTable.easy_getCell(8,2).setValue("July" )
xlsFirstTable.easy_getCell(8,3).setValue("2011" )
xlsFirstTable.easy_getCell(8,4).setValue("360" )
xlsPivotTable = gateway.jvm.ExcelPivotTable()
xlsPivotTable.setName("Sales" )
xlsPivotTable.setSourceRange("First tab!$A$1:$E$9" , workbook)
xlsPivotTable.setLocation("A3:G15" )
xlsPivotTable.addFieldToRowLabels("Sale agent" )
xlsPivotTable.addFieldToColumnLabels("Year" )
xlsPivotTable.addFieldToValues("Sale amount" ,"Sale amount per year" , gateway.jvm.PivotTable.SUBTOTAL_SUM)
xlsPivotTable.addFieldToReportFilter("Sale country" )
xlsPivotTable.setOutlineForm()
xlsPivotTable.setStyle(gateway.jvm.PivotTable.PIVOT_STYLE_DARK_11)
workbook.easy_getSheet("Second tab" ).easy_addPivotTable(xlsPivotTable)
print("Writing file C:\\Samples\\Tutorial25 - pivot table in Excel.xlsx." )
workbook.easy_WriteXLSXFile("C:\\Samples\\Tutorial25 - pivot table in Excel.xlsx" )
sError = workbook.easy_getError()
if sError == "" :
print("\nFile successfully created.\n" )
else :
print("\nError encountered: " + sError + "\n\n" )
gc.collect()