Questo breve tutorial indirizza a creare una tabella pivot in Python con l’aiuto di passaggi dettagliati che forniscono informazioni sulla configurazione dell’ambiente e sul flusso del programma Python. Durante l’utilizzo di Python Excel tabella pivot verrà creata utilizzando un codice di esempio eseguibile dopo aver caricato la cartella di lavoro di origine contenente i dati di input per la tabella pivot. Alla fine, la cartella di lavoro risultante verrà salvata in uno qualsiasi dei formati desiderati come XLSX, XLS, ecc.
Passaggi per creare una tabella pivot in Python
- Stabilire l’ambiente per installare Aspose.Cells per Python tramite Java nel progetto
- Carica o crea un oggetto classe Workbook contenente dati di input per la tabella pivot
- Ottieni il riferimento alla raccolta di tabelle pivot nella destinazione worksheet
- Aggiungi una tabella pivot nella raccolta
- Configura la tabella pivot appena aggiunta
- Aggiungi i campi desiderati nelle rispettive aree della tabella pivot
- Salva la cartella di lavoro di output con la tabella pivot al suo interno
Questi passaggi forniscono istruzioni per il codice Python per creare una tabella pivot in Excel condividendo un collegamento alla risorsa di configurazione dell’ambiente e a una sequenza di attività da eseguire in Python per ottenere la funzionalità. Guida per aggiungere campi a diverse aree nella tabella pivot secondo il requisito. Una volta preparata la tabella pivot, viene salvata nel file Excel nel formato desiderato.
Codice per creare tabelle pivot di Excel con Python
import jpype | |
import csv | |
import asposecells | |
jpype.startJVM() | |
from asposecells.api import License, Workbook, PivotFieldType, LoadOptions,FileFormatType | |
# Instantiate a license to avoid watermark in the output Excel file having pivot table | |
cellsLicense = License() | |
cellsLicense.setLicense("Aspose.Cells.lic") | |
header = ['City', 'Class', 'Fee'] | |
data = [ | |
['Islamabad','Class 1',750], | |
['Islamabad','Class 4',1000], | |
['Karachi','Class 1',300], | |
['Karachi','Class 4',750], | |
['Karachi','Class 1',2035], | |
['Karachi','Class 4',2500], | |
['Islamabad','Class 1',3215] | |
] | |
with open('data.csv', 'w', encoding='UTF8', newline='') as f: | |
writer = csv.writer(f) | |
# write the header | |
writer.writerow(header) | |
# write the data | |
writer.writerows(data) | |
# Create a CSV LoadOptions class object | |
csvLoadOptions = LoadOptions(FileFormatType.CSV) | |
# Load the CSV data into Workbook class object using the load options | |
csvWorkbook = Workbook("data.csv",csvLoadOptions) | |
# Get access to the first sheet for adding pivot table to it | |
wsPivotTable = csvWorkbook.getWorksheets().get(0) | |
# Get access to pivot tables collection in the selected sheet | |
pivotTablesCollection = wsPivotTable.getPivotTables() | |
# Create the pivot table and save its index | |
pivotTableIndex = pivotTablesCollection.add("=A1:C8", "A10", "PythonPivotTable") | |
# Get access to the newly created pivot table | |
newPivotTable = pivotTablesCollection.get(pivotTableIndex) | |
# set flag to hide grand totals for rows | |
newPivotTable.setRowGrand(False) | |
# Add the first field to the column area of the pivot table | |
newPivotTable.addFieldToArea(PivotFieldType.COLUMN, 0) | |
# Add the second field to the row area of the pivot table | |
newPivotTable.addFieldToArea(PivotFieldType.ROW, 1) | |
# Add the third field to the data area | |
newPivotTable.addFieldToArea(PivotFieldType.DATA, 2) | |
# Saving the Excel file | |
csvWorkbook.save("NewPivotTable.xlsx") | |
jpype.shutdownJVM() |
Questo articolo ci ha guidato a creare una tabella pivot. Se vuoi leggere file Excel protetti da password, fai riferimento all’articolo su leggere il file Excel protetto da password in Python.