PythonのXlsxWriterモジュールでExcelシートにドーナツチャートを作成する方法
ドーナツチャート(Doughnut Chart)は、円グラフ(Pie Chart)をアレンジしたグラフの一種です。中央に丸い穴があり、見た目がドーナツに似ていることからこの名前が付けられました。この中央の空きスペースを利用すれば、合計値や補足情報などの追加データを表示することも可能です。
サンプルコード
以下のコードでは、XlsxWriterモジュールを使用してワークブックを作成し、ドーナツチャートをExcelシートに挿入しています。
# import xlsxwriter module
import xlsxwriter
# Workbook() takes one, non-optional, argument which is the filename #that we want to create.
workbook = xlsxwriter.Workbook('chart_doughnut1.xlsx')
# The workbook object is then used to add new worksheet via the #add_worksheet() method.
worksheet = workbook.add_worksheet()
# Create a new Format object to formats cells in worksheets using
# add_format() method .
# here we create bold format object .
bold = workbook.add_format({'bold': 1})
# Add the worksheet data that the charts will refer to.
headings = ['Category', 'Values']
data = [
['Glazed', 'Chocolate', 'Cream'],
[50, 35, 15],
]
# Write a row of data starting from 'A1' with bold format .
worksheet.write_row('A1', headings, bold)
# Write a column of data starting from 'A2', 'B2' respectively .
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
# Create a chart object that can be added to a worksheet using #add_chart() method.
# here we create a doughnut chart object .
chart1 = workbook.add_chart({'type': 'doughnut'})
# Add a data series to a chart using add_series method.
# Configure the first series.syntax to define ranges
# [sheetname, first_row, first_col, last_row, last_col].
chart1.add_series({
'name': 'Doughnut sales data',
'categories': ['Sheet1', 1, 0, 3, 0],
'values': ['Sheet1', 1, 1, 3, 1],
})
# Add a chart title
chart1.set_title({'name': 'Popular Doughnut Types'})
# Set an Excel chart style. Colors with white outline and shadow.
chart1.set_style(10)
# add chart to the worksheet with an offset,at the top-left corner #of a chart is anchored to cell C2.
worksheet.insert_chart('C2', chart1, {'x_offset': 25, 'y_offset': 10})
# Finally, close the Excel file via the close() method.
workbook.close()
コードのポイント解説
- Workbook()メソッド: 引数に指定したファイル名(chart_doughnut1.xlsx)で新しいExcelファイルを作成します。
- add_format()メソッド: セルの書式を定義するFormatオブジェクトを生成します。ここでは太字(bold)スタイルを作成し、見出し行に適用しています。
- write_row() / write_column()メソッド: 指定したセルを起点として、行方向・列方向にデータを書き込みます。
- add_chart()メソッド: グラフタイプを「doughnut」と指定して、ドーナツチャートのオブジェクトを作成します。
- add_series()メソッド: グラフのデータ系列を設定します。範囲は[シート名, 開始行, 開始列, 終了行, 終了列]の形式で指定します。
- insert_chart()メソッド: セルC2を基準にオフセットを指定して、ワークシート上にグラフを挿入します。
- close()メソッド: 最後に必ず呼び出してExcelファイルを保存・閉じます。
スクリプトを実行すると、カレントディレクトリに「chart_doughnut1.xlsx」が生成され、開くとセルC2の位置にドーナツチャートが表示されます。XlsxWriterはインストールが必要な場合は pip install XlsxWriter コマンドで導入できます。
-
PythonのXlsxWriterモジュールでExcelワークシートにグラフを挿入する方法
Pythonには標準ライブラリ以外にも、個人開発者によって作られた優れた外部ライブラリが数多く存在し、Pythonにさまざまな追加機能をもたらしています。XlsxWriterもそのひとつで、Pythonプログラムのデータを含むExcelファイルを作成できるだけでなく、グラフ(チャート)の作成にも対応しています。 円グラフの作成手順 以下の例では、xlsxwriterを使って円グラフを作成します。処理の流れは以下のとおりです。 まずワークブックを定義し、その後にワークシートを追加します。 次にプロットするデータを定義し、Excelファイルのどの列にデータを書き込むかを指定します。 その列を参照
-
PythonのopenpyxlモジュールでExcelファイルを読み書きする方法【初心者向け完全ガイド】
Pythonには、Excelファイル(.xlsx)を操作するためのopenpyxlという便利なモジュールが用意されています。このモジュールを使えば、Excelファイルの新規作成、データの書き込み・読み取り、シートの追加や管理など、さまざまな操作をプログラムから自動的に行うことができます。 本記事では、openpyxlの基本的な使い方を、実際に動作するサンプルコードとともに順番に解説していきます。 openpyxlのインストール方法 openpyxlは標準ライブラリではないため、まずインストールが必要です。コマンドプロンプト(またはターミナル)で以下のコマンドを実行してください。 pip ins