สร้างแผนภูมิวงกลมใน Word โดยใช้ Python

แผนภูมิวงกลมใน Word เป็นเครื่องมือเชิงภาพที่ทรงพลัง ช่วยในการแสดงข้อมูลในรูปแบบที่ชัดเจนและน่าสนใจ หลายอุตสาหกรรมใช้แผนภูมิวงกลมเพื่อแสดงสัดส่วนและการเปรียบเทียบ ตัวอย่างเช่น ธุรกิจใช้เพื่อแสดงข้อมูลการขาย นักการศึกษานำไปใช้เพื่อแสดงผลสำรวจ แผนภูมิวงกลมที่ออกแบบมาอย่างดีสามารถเพิ่มคุณค่าให้กับรายงานและการนำเสนอ ทำให้ข้อมูลที่ซับซ้อนเข้าใจง่ายขึ้น ในโพสต์บล็อกนี้ เราจะสำรวจวิธีการสร้างแผนภูมิวงกลมใน Word โดยใช้ Python

บทความนี้ครอบคลุมหัวข้อต่อไปนี้:

ไลบรารีแผนภูมิวงกลม Python Word

เราจะใช้ Aspose.Words สำหรับ Python เพื่อสร้างแผนภูมิวงกลมในเอกสาร Word ซึ่งช่วยให้กระบวนการสร้างและแก้ไขเอกสาร Word ง่ายขึ้น ด้วย Aspose.Words คุณสามารถแทรกแผนภูมิได้อย่างง่ายดาย รวมถึงแผนภูมิวงกลมในเอกสารของคุณ ไลบรารีนี้รองรับประเภทแผนภูมิที่หลากหลายและมีตัวเลือกการปรับแต่งขั้นสูง ทำให้เป็นตัวเลือกที่ยอดเยี่ยมสำหรับนักพัฒนาที่ต้องการทำให้การสร้างเอกสารเป็นอัตโนมัติ

ไลบรารีแผนภูมิวงกลม Python นี้มีฟีเจอร์หลายอย่างที่ทำให้เหมาะสำหรับการสร้างแผนภูมิวงกลมใน Word:

  • ความสะดวกในการรวม: ไลบรารีรวมเข้ากับแอปพลิเคชัน Python ได้อย่างราบรื่น
  • ความยืดหยุ่น: คุณสามารถสร้าง แก้ไข และจัดการเอกสารได้ตามโปรแกรม
  • ตัวเลือกการปรับแต่งขั้นสูง: ปรับแต่งรูปลักษณ์และข้อมูลของแผนภูมิวงกลมได้อย่างง่ายดาย
  • ประสิทธิภาพสูง: สร้างเอกสารได้อย่างรวดเร็วโดยไม่ลดทอนคุณภาพ

ในการเริ่มต้น คุณต้องติดตั้ง Aspose.Words สำหรับ Python คุณสามารถดาวน์โหลดได้จาก ส่วนการปล่อย ใช้คำสั่ง pip ต่อไปนี้เพื่อติดตั้ง:

pip install aspose-words

วิธีสร้างแผนภูมิวงกลมใน Word

ติดตามขั้นตอนเหล่านี้เพื่อสร้างแผนภูมิวงกลมในเอกสาร Word โดยใช้ Python:

  1. สร้างเอกสาร Word ใหม่โดยใช้คลาส Document
  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")
วิธีสร้างแผนภูมิวงกลมใน Word โดยใช้ Python

วิธีสร้างแผนภูมิวงกลมใน Word โดยใช้ Python.

สร้างแผนภูมิ Pie of Pie ใน Word

ในการสร้างแผนภูมิ “Pie of Pie” แทนที่จะเป็นแผนภูมิพายมาตรฐาน คุณเพียงแค่ต้องระบุ ChartType.PIE_OF_PIE แทน ChartType.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");
วิธีสร้างแผนภูมิ Pie of Pie ใน Word โดยใช้ Python

วิธีสร้างแผนภูมิ Pie of Pie ใน Word โดยใช้ Python.

สร้างแผนภูมิ Pie of Bar ใน Word

ในการสร้างแผนภูมิ “Pie of Bar” คุณเพียงแค่ต้องปรับเปลี่ยนขั้นตอนที่ 2 เพื่อใช้ ChartType.PIE_OF_BAR แทน ChartType.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
# 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")
วิธีสร้างแผนภูมิ Pie of Bar ใน Word โดยใช้ Python

วิธีสร้างแผนภูมิ Pie of Bar ใน Word โดยใช้ Python.

สร้างแผนภูมิ 3D Pie ใน Word

ในการสร้างแผนภูมิพาย 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")
วิธีสร้างแผนภูมิ 3D Pie ใน Word โดยใช้ Python

วิธีสร้างแผนภูมิ 3D Pie ใน Word โดยใช้ Python.

รับใบอนุญาตฟรี

สนใจสำรวจผลิตภัณฑ์ Aspose หรือไม่? เยี่ยมชม หน้าใบอนุญาต เพื่อขอรับใบอนุญาตชั่วคราวฟรีสำหรับ Aspose.Words สำหรับ Python ง่ายต่อการเริ่มต้นและสัมผัสประสบการณ์การทำงานของไลบรารีอย่างเต็มที่ ห้ามพลาดโอกาสนี้!

แผนภูมิพายใน Word: แหล่งข้อมูลฟรี

นอกเหนือจากการสร้างแผนภูมิพายในเอกสาร Word เรายังขอแนะนำให้คุณสำรวจแหล่งข้อมูลเพิ่มเติม แหล่งข้อมูลเหล่านี้จะช่วยให้คุณเสริมสร้างความเข้าใจและทักษะกับ Aspose.Words สำหรับ Python

สรุป

ในโพสต์บล็อกนี้ เราได้สำรวจวิธีการสร้างแผนภูมิพายใน Word โดยใช้ Python เราได้พูดคุยเกี่ยวกับฟีเจอร์ของไลบรารีและให้คำแนะนำแบบทีละขั้นตอนพร้อมตัวอย่างโค้ด ด้วย Aspose.Words คุณสามารถทำให้การสร้างเอกสารเป็นอัตโนมัติและเพิ่มมุมมองข้อมูลในรายงานของคุณ สำรวจเพิ่มเติมเกี่ยวกับ Aspose.Words สำหรับ Python เพื่อปลดล็อกศักยภาพของมันอย่างเต็มที่

หากคุณมีคำถามหรือต้องการความช่วยเหลือเพิ่มเติม โปรดติดต่อเราที่ ฟอรัมสนับสนุนฟรี

ดูเพิ่มเติม