Tutorial singkat ini mengarahkan ke membuat tabel pivot dengan Python dengan bantuan langkah-langkah mendetail yang memberikan informasi tentang konfigurasi lingkungan dan alur program Python. Saat menggunakan Python Excel tabel pivot akan dibuat menggunakan kode contoh yang dapat dijalankan setelah memuat buku kerja sumber yang berisi data input untuk tabel pivot. Pada akhirnya, buku kerja yang dihasilkan akan disimpan dalam format yang diinginkan seperti XLSX, XLS, dll.
Langkah-langkah Membuat Tabel Pivot dengan Python
- Tetapkan lingkungan untuk menginstal Aspose.Cells untuk Python melalui Java ke dalam proyek
- Muat atau buat objek kelas Workbook yang berisi data input untuk tabel pivot
- Dapatkan referensi ke kumpulan tabel pivot di target worksheet
- Tambahkan tabel pivot dalam koleksi
- Konfigurasikan tabel pivot yang baru ditambahkan
- Tambahkan bidang yang diinginkan ke area masing-masing di tabel pivot
- Simpan buku kerja keluaran dengan tabel pivot di dalamnya
Langkah-langkah ini memberikan instruksi untuk kode Python untuk membuat tabel pivot di Excel dengan membagikan tautan ke sumber daya konfigurasi lingkungan dan ke urutan tugas yang harus dilakukan dengan Python untuk mencapai fungsionalitas. Ini memandu untuk menambahkan bidang ke area yang berbeda di tabel pivot sesuai kebutuhan. Setelah tabel pivot disiapkan, tabel tersebut disimpan dalam file Excel dalam format yang diinginkan.
Kode untuk Membuat Tabel Pivot Excel dengan 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() |
Artikel ini telah memandu kita untuk membuat tabel pivot. Jika Anda ingin membaca file Excel yang dilindungi kata sandi, lihat artikel di baca file Excel yang dilindungi kata sandi dengan Python.