บทช่วยสอนสั้นๆ นี้ชี้นำไปยัง สร้าง pivot table ใน Python ด้วยความช่วยเหลือของขั้นตอนโดยละเอียดที่ให้ข้อมูลเกี่ยวกับการกำหนดค่าสภาพแวดล้อมและโฟลว์ของโปรแกรม Python ขณะใช้ Python Excel pivot table จะถูกสร้างขึ้นโดยใช้โค้ดตัวอย่างที่เรียกใช้ได้หลังจากโหลดเวิร์กบุ๊กต้นฉบับที่มีข้อมูลอินพุตสำหรับตาราง Pivot ในตอนท้าย สมุดงานผลลัพธ์จะถูกบันทึกในรูปแบบใดก็ได้ที่ต้องการ เช่น XLSX, XLS ฯลฯ
ขั้นตอนในการสร้าง Pivot Table ใน Python
- สร้างสภาพแวดล้อมเพื่อติดตั้ง Aspose.Cells สำหรับ Python ผ่าน Java เข้าไปในโครงการ
- โหลดหรือสร้างวัตถุคลาส Workbook ที่มีข้อมูลอินพุตสำหรับตารางเดือย
- รับข้อมูลอ้างอิงไปยังคอลเลกชันตารางเดือยในเป้าหมาย worksheet
- เพิ่มตารางเดือยในคอลเลกชัน
- กำหนดค่าตารางเดือยที่เพิ่มเข้ามาใหม่
- เพิ่มฟิลด์ที่ต้องการลงในพื้นที่ตามลำดับในตารางเดือย
- บันทึกสมุดงานออกด้วยตารางเดือยในนั้น
ขั้นตอนเหล่านี้ให้คำแนะนำสำหรับ โค้ด Python เพื่อสร้างตาราง Pivot ใน Excel โดยการแชร์ลิงก์ไปยังทรัพยากรการกำหนดค่าสภาพแวดล้อมและลำดับของงานที่ต้องทำใน Python เพื่อให้ได้ฟังก์ชันการทำงาน เป็นคำแนะนำในการเพิ่มเขตข้อมูลไปยังพื้นที่ต่างๆ ในตารางเดือยตามข้อกำหนด เมื่อเตรียม pivot table แล้ว จะบันทึกลงในไฟล์ Excel ในรูปแบบที่ต้องการ
รหัสเพื่อสร้าง Excel Pivot Table ด้วย 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() |
บทความนี้แนะนำเราในการสร้างตารางเดือย หากคุณต้องการอ่านไฟล์ Excel ที่ป้องกันด้วยรหัสผ่าน โปรดดูบทความเรื่อง {{ไฮเปอร์ลิงก์1}}