Pythonを使用してWordに円グラフを作成する

Wordの円グラフは、強力な視覚ツールです。データを明確かつ魅力的な方法で表現するのに役立ちます。多くの業界が、比率や比較を表示するために円グラフを使用しています。たとえば、企業は売上データに使用し、教育者は調査結果に使用します。よく設計された円グラフは、レポートやプレゼンテーションを強化できます。複雑な情報を理解しやすくします。このブログ投稿では、Pythonを使用してWordに円グラフを作成する方法を探ります。

この記事では、以下のトピックを扱います:

Python円グラフWordライブラリ

Word文書に円グラフを作成するために、Aspose.Words for Pythonを使用します。これにより、Word文書の作成と編集のプロセスが簡素化されます。Aspose.Wordsを使用すると、円グラフを含むチャートを簡単に文書に挿入できます。このライブラリはさまざまなチャートタイプをサポートし、高度なカスタマイズオプションを提供します。これにより、ドキュメント生成を自動化したい開発者にとって優れた選択肢となります。

このPython円グラフライブラリは、Wordに円グラフを作成するのに理想的な多くの機能を提供します:

  • 統合の容易さ:ライブラリはPythonアプリケーションとスムーズに統合されます。
  • 柔軟性:プログラムで文書を作成、変更、操作できます。
  • 高度なカスタマイズオプション:円グラフの外観やデータを簡単にカスタマイズできます。
  • 高性能:品質を損なうことなく、迅速に文書を生成します。

始めるには、Aspose.Words for Pythonをインストールする必要があります。リリースセクションからダウンロードできます。次のpipコマンドを使用してインストールしてください:

pip install aspose-words

Wordに円グラフを作成する方法

Pythonを使用してWord文書に円グラフを作成するには、次の手順に従います:

  1. Documentクラスを使用して新しいWord文書を作成します。
  2. 文書を構築するためのDocumentBuilderオブジェクトを作成します。
  3. **insert_chart()**メソッドを使用して、指定された寸法の円グラフを挿入します。
  4. chartShape.chartを使用して、挿入されたチャートシェイプからチャートを取得します。
  5. **chart.series.clear()**を使用して、チャート内の既存のシリーズをクリアします。
  6. カテゴリと値を持つ新しいシリーズを追加します。
  7. データラベルをカスタマイズします。
  8. 外観をカスタマイズします。
  9. **save()**メソッドを使用して、指定されたパスに文書を保存します。

以下は、上記の手順を実装した完全なPythonコードの例です:

import aspose.words as aw
import aspose.pydrawing as drawing
from aspose.words import Document, DocumentBuilder
from aspose.words.drawing import Shape
from aspose.words.drawing.charts import ChartType
# Step 1: Create a Document
doc = aw.Document()
# Step 2: Add a Pie Chart
builder = aw.DocumentBuilder(doc)
chartShape = builder.insert_chart(ChartType.PIE, 400, 300)
chart = chartShape.chart;
# Step 3: Set Data
chart.series.clear()
series = chart.series.add("Aspose Series 1",
["Category 1", "Category 2", "Category 3"],
[2.7, 3.2, 0.8])
labels = series.data_labels
labels.show_percentage = True
labels.show_value = True
labels.show_leader_lines = False
labels.separator = " - "
# Step 4: Customize Appearance
# Give our chart a title, which appears at the top center of the chart area.
title = chart.title;
title.text = "Sample Pie Chart";
title.font.size = 15;
title.font.color = drawing.Color.blue;
title.show = True;
title.overlay = True;
# Step 5: Save the Document
doc.save("PieChartInWord.docx")
Pythonを使用してWordに円グラフを作成する方法

Pythonを使用してWordに円グラフを作成する方法。

Wordでパイ・オブ・パイチャートを作成する

標準のパイチャートの代わりに「パイ・オブ・パイ」チャートを作成するには、ChartType.PIE_OF_PIEを使用するだけです。

以下は、この修正を加えたコードです:

import aspose.words as aw
import aspose.pydrawing as drawing
from aspose.words import Document, DocumentBuilder
from aspose.words.drawing import Shape
from aspose.words.drawing.charts import ChartType
# Create a document.
doc = aw.Document();
builder = aw.DocumentBuilder(doc);
# Insert a Pio of Pie chart
shape = builder.insert_chart(ChartType.PIE_OF_PIE, 440, 300);
chart = shape.chart;
# Delete the default generated series.
chart.series.clear();
# Add series
series = chart.series.add("Aspose Series 1",
["Category 1", "Category 2", "Category 3", "Category 4"],
[11, 8, 4, 3])
# Format the Pie of Pie chart.
seriesGroup = chart.series_groups[0];
seriesGroup.gap_width = 10;
seriesGroup.second_section_size = 77;
doc.save("PieOfPieChart.docx");
Pythonを使用してWordでパイ・オブ・パイチャートを作成する方法

Pythonを使用してWordでパイ・オブ・パイチャートを作成する方法。

Wordでパイ・オブ・バー・チャートを作成する

「パイ・オブ・バー」チャートを作成するには、ステップ2を修正してChartType.PIE_OF_BARを使用するだけです。以下は、更新されたコードです:

import aspose.words as aw
import aspose.pydrawing as drawing
from aspose.words import Document, DocumentBuilder
from aspose.words.drawing import Shape
from aspose.words.drawing.charts import ChartType
# Step 1: Create a Document
doc = aw.Document()
# Step 2: Add a Pie of Bar Chart
builder = aw.DocumentBuilder(doc)
chartShape = builder.insert_chart(ChartType.PIE_OF_BAR, 400, 300) # Change to PIE_OF_BAR
chart = chartShape.chart
# Step 3: Set Data
chart.series.clear()
series = chart.series.add("Aspose Series 1",
["Category 1", "Category 2", "Category 3", "Category 4", "Category 5"],
[2.7, 3.2, 0.8, 1.2, 0.5])
labels = series.data_labels
labels.show_percentage = True
labels.show_value = True
labels.show_leader_lines = False
labels.separator = " - "
# Step 4: Customize Appearance
title = chart.title
title.text = "Sample Pie of Bar Chart"
title.font.size = 15
title.font.color = drawing.Color.blue
title.show = True
title.overlay = True
# Step 5: Save the Document
doc.save("PieOfBarChartInWord.docx")
Pythonを使用してWordでパイ・オブ・バー・チャートを作成する方法

Pythonを使用してWordでパイ・オブ・バー・チャートを作成する方法。

Wordで3Dパイチャートを作成する

3Dパイチャートを作成するには、ステップ2を更新してChartType.PIE_3Dを指定するだけです。以下は、更新されたコードです:

import aspose.words as aw
import aspose.pydrawing as drawing
from aspose.words import Document, DocumentBuilder
from aspose.words.drawing import Shape
from aspose.words.drawing.charts import ChartType
# Step 1: Create a Document
doc = aw.Document()
# Step 2: Add a 3D Pie Chart
builder = aw.DocumentBuilder(doc)
chartShape = builder.insert_chart(ChartType.PIE_3D, 400, 300) # Change to PIE_3D
chart = chartShape.chart
# Step 3: Set Data
chart.series.clear()
series = chart.series.add("Aspose Series 1",
["Category 1", "Category 2", "Category 3"],
[2.7, 3.2, 0.8])
labels = series.data_labels
labels.show_percentage = True
labels.show_value = True
labels.show_leader_lines = False
labels.separator = " - "
# Step 4: Customize Appearance
chart.title.text = "Sample 3D Pie Chart"
# Step 5: Save the Document
doc.save("3DPieChartInWord.docx")
Pythonを使用してWordで3Dパイチャートを作成する方法

Pythonを使用してWordで3Dパイチャートを作成する方法。

無料ライセンスを取得する

Aspose製品を探索したいですか? ライセンスページを訪れて、Aspose.Words for Pythonの無料の一時ライセンスを取得してください。簡単に始められ、ライブラリの全機能を体験できます。この機会をお見逃しなく!

Wordでのパイチャート:無料リソース

Word文書でパイチャートを作成するだけでなく、追加のリソースを探索することをお勧めします。これらのリソースは、Aspose.Words for Pythonに関する理解とスキルをさらに向上させるのに役立ちます。

結論

このブログ記事では、Pythonを使用してWordでパイチャートを作成する方法を探求しました。ライブラリの機能について議論し、コード例を伴ったステップバイステップガイドを提供しました。Aspose.Wordsを使用すれば、ドキュメント作成を簡単に自動化し、視覚的データでレポートを強化できます。Aspose.Words for Pythonについてさらに探求し、その全潜在能力を引き出してください。

質問がある場合やさらなるサポートが必要な場合は、無料サポートフォーラムでお気軽にお問い合わせください。

その他