Šiame trumpame vadove nurodoma sukurti suvestinę lentelę Python naudojant išsamius veiksmus, kuriuose pateikiama informacija apie aplinkos konfigūraciją ir Python programos eigą. Naudojant Python Excel suvestinę lentelę bus sukurta naudojant paleidžiamą pavyzdinį kodą įkėlus šaltinio darbaknygę, kurioje yra suvestinės lentelės įvesties duomenys. Galų gale gauta darbaknygė bus išsaugota bet kuriuo norimu formatu, pvz., XLSX, XLS ir kt.
Veiksmai, kaip sukurti Pivot lentelę Python.
- Sukurkite aplinką, kad įdiegtumėte Aspose.Cells for Python per Java į projektą
- Įkelkite arba sukurkite Workbook klasės objektą su suvestinės lentelės įvesties duomenimis
- Gaukite nuorodą į suvestinių lentelių rinkinį tikslinėje worksheet
- Pridėkite suvestinę lentelę į kolekciją
- Konfigūruokite naujai pridėtą suvestinę lentelę
- Pridėkite norimus laukus į atitinkamas suvestinės lentelės sritis
- Išsaugokite išvesties darbaknygę su joje esančia sukimo lentele
Šie veiksmai pateikia nurodymus, kaip Python kodui kurti suvestinę lentelę programoje Excel, bendrinant nuorodą į aplinkos konfigūracijos išteklius ir užduočių, kurias reikia atlikti Python, seką, kad būtų pasiektas funkcionalumas. Jame nurodoma, kaip pridėti laukus į skirtingas suvestinės lentelės sritis pagal reikalavimą. Parengus suvestinę lentelę, ji išsaugoma Excel faile norimu formatu.
Kodas, skirtas sukurti “Excel Pivot” lentelę naudojant 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() |
Šis straipsnis padėjo mums sukurti suvestinę lentelę. Jei norite skaityti slaptažodžiu apsaugotus Excel failus, skaitykite straipsnį apie skaityti slaptažodžiu apsaugotą Excel failą Python..