この簡単なチュートリアルでは、環境構成とPythonプログラムフローに関する情報を提供する詳細な手順を使用して、Pythonでピボットテーブルを作成する方法について説明します。 ** Python Excelピボットテーブル**を使用している間、ピボットテーブルの入力データを含むソースワークブックを読み込んだ後、実行可能なサンプルコードを使用して作成されます。最終的に、作成されたワークブックは、XLSX、XLSなどの任意の形式で保存されます。
Pythonでピボットテーブルを作成する手順
- Java経由でPython用のAspose.Cellsをインストールするための環境を確立しますをプロジェクトに追加
- ピボットテーブルの入力データを含むWorkbookクラスオブジェクトをロードまたは作成します
- ターゲットworksheetのピボットテーブルコレクションへの参照を取得します
- コレクションにピボットテーブルを追加します
- 新しく追加されたピボットテーブルを構成します
- ピボットテーブルのそれぞれの領域に目的のフィールドを追加します
- ピボットテーブルを含む出力ブックを保存します
これらの手順は、環境構成リソースへのリンクと、機能を実現するためにPythonで実行される一連のタスクへのリンクを共有することにより、PythonコードがExcelでピボットテーブルを作成するための手順を提供します。要件に応じて、ピボットテーブルのさまざまな領域にフィールドを追加するためのガイドです。ピボットテーブルが準備されると、Excelファイルに目的の形式で保存されます。
PythonでExcelピボットテーブルを作成するコード
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() |
- Pythonのこれらのコード行は、ロードされたExcelファイルのデータを使用してExcelピボットテーブル*を作成します。データを含むExcelファイルが既にある場合は、ブックの作成をスキップしてロードするだけでよいことに注意してください。このコードは、RowGrandフラグをFalseに設定して個々の行の総計を非表示にすることで、新しく作成されたピボットテーブルのカスタマイズも示しています。一方、変更テキストのタイトルの設定、列のグランドの設定、カスタムリストの並べ替えなどの他のカスタマイズも実行できます。 、など。
この記事では、ピボットテーブルを作成する方法について説明しました。パスワードで保護されたExcelファイルを読みたい場合は、次の記事を参照してください。 パスワードで保護されたExcelファイルをPythonで読み取る。