يوجه هذا البرنامج التعليمي الموجز إلى ** إنشاء جدول محوري في Python ** بمساعدة الخطوات التفصيلية التي توفر معلومات حول تكوين البيئة وتدفق برنامج Python. أثناء استخدام ** سيتم إنشاء جدول محوري Python Excel ** باستخدام نموذج تعليمة برمجية قابلة للتشغيل بعد تحميل المصنف المصدر الذي يحتوي على بيانات الإدخال للجدول المحوري. في النهاية ، سيتم حفظ المصنف الناتج بأي من التنسيقات المرغوبة مثل XLSX ، XLS ، إلخ.
خطوات إنشاء جدول محوري في بايثون
- قم بتأسيس البيئة لتثبيت Aspose.Cells for Python عبر Java في المشروع
- قم بتحميل أو إنشاء كائن فئة Workbook يحتوي على بيانات الإدخال للجدول المحوري
- احصل على مرجع لمجموعة الجداول المحورية في الهدف worksheet
- أضف جدول محوري في المجموعة
- تكوين الجدول المحوري المضاف حديثًا
- أضف الحقول المطلوبة إلى المناطق المعنية في الجدول المحوري
- احفظ مصنف الإخراج مع الجدول المحوري فيه
توفر هذه الخطوات تعليمات لـ * تعليمات برمجية Python لإنشاء جدول محوري في Excel * من خلال مشاركة ارتباط إلى مورد تكوين البيئة وتسلسل المهام التي يجب إجراؤها في Python لتحقيق الوظيفة. يرشد لإضافة حقول إلى مناطق مختلفة في الجدول المحوري وفقًا للمتطلبات. بمجرد إعداد الجدول المحوري ، يتم حفظه في ملف Excel بالتنسيق المطلوب.
كود لإنشاء جدول Excel المحوري باستخدام 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 المحمية بكلمة مرور ، فراجع المقالة الموجودة على قراءة ملف Excel المحمي بكلمة مرور في Python.