Introduction
คู่มือนี้แสดงวิธีการสร้าง ปรับเปลี่ยน และบันทึกภาพนําเสนอ PowerPoint โดยใช้โปรแกรมใน Python โดยใช Aspose.Slides FOSS. ห้องสมุดจะให้คุณสามารถสร้างสไลด์จากเดิม, เพิ่มและจัดสรรใหม่ สไลด, เติมรูปแบบ, ตาราง, และข้อความที่ถูกประกอบเป็นลักษณะ, แล้วเก็บผลงานไปยัง PPTX. การทํางานทั้งหมดเกิดขึ้นในไพธอนบริสุทธิ์โดยไม่ต้องขึ้นอยู่กับ Microsoft Office หรือขั้นสองใด ๆ ทางภายนอก.
ห้องสมุดนี้มีอยู่บน PyPI ในรูปแบบ: aspose-slides-foss ภายใต้ลิขสิทธิ์ MIT. ติดตั้ง มันกับ pip, นําเข้า และเริ่มสร้างการนําเสนอในโค้ดสองสามแถว. คุณกําลังทําการสร้างรายงานโดยอัตโนมัติ, สร้างท่อสไลด์เด็ค หรือบูจุ การนําเสนอผลิตในแอพลิเคชั่นเว็บ API ครอบคลุมพื้นที่ทั้งหมด: slide ภาพรวม, ไส้แบบหลัก/การวางแผน, รูปภาพสะสม, เฟรมข้อความ, แบบที่เติม และ ทางเลือกส่งออก.
บทความนี้จะนําเสนอถึง ความสามารถในการจัดการการแสดงตัวหลัก คลาสและวิธีที่คุณใช้มากที่สุดเมื่อทํางานกับภาพถ่ายใน Python.
สิ่งที่รวมอยู่ใน
การนําเสนอวงจรชีวิต
การ Presentation class เป็นจุดเข้า. สร้าง Deck ใหม่, ปรับเปลี่ยนเนื้อหาของมัน และบันทึกเป็น PPTX ใช้ Presentation ในฐานะผู้จัดการแวดล้อมกับ with เพื่อปล่อยตัวไฟล์ได้โดยอัตโนมัติ.
import io
from aspose.slides_foss import Presentation
from aspose.slides_foss.export import SaveFormat
with Presentation() as pres:
# A new presentation starts with one blank slide
print("Slides:", len(pres.slides)) # 1
# Save to a file
pres.save("output.pptx", SaveFormat.PPTX)
# Or save to a stream
buf = io.BytesIO()
pres.save(buf, SaveFormat.PPTX)
การเก็บภาพสไลด์
SlideCollection ให้คุณเพิ่ม, ใส่, คลอน, ลบ และเรียงใหม่สไลด์. len(pres.slides) แสดงการนับตัวต่อเนื่องหลังจากแต่ละครั้งที่เปลี่ยน.
from aspose.slides_foss import Presentation, ShapeType
with Presentation() as pres:
layout = pres.layout_slides[0]
# Add a blank slide using the first layout
pres.slides.add_empty_slide(layout)
# Insert at a specific position
pres.slides.insert_empty_slide(1, layout)
# Clone an existing slide (shapes are copied)
pres.slides[0].shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 200, 100)
pres.slides.add_clone(pres.slides[0])
print("Total slides:", len(pres.slides)) # 4
# Remove by index
pres.slides.remove_at(3)
รูปแบบและอัตโนมัติรุ่น
เพิ่มสี่เหลี่ยม, กลิปวง, สามเหอหนานและรูปทรงทางภูมิศาสตร์อื่น ๆ ไปยังการถ่ายภาพใดๆผ่าน ShapeCollection.add_auto_shape().ทุกรูปแบบแสดงตําแหน่ง ขนาด การหมุน, และคุณสมบัติที่เต็ม ที่คงอยู่ตลอดรอบการเก็บ / รีโลด.
from aspose.slides_foss import Presentation, ShapeType
with Presentation() as pres:
slide = pres.slides[0]
slide.shapes.clear()
rect = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 300, 150)
rect.rotation = 15
ellipse = slide.shapes.add_auto_shape(ShapeType.ELLIPSE, 400, 50, 200, 200)
# Reorder z-index
slide.shapes.reorder(0, ellipse)
print("Front shape:", slide.shapes[0].shape_type) # ELLIPSE
Tables
สร้างตารางข้อมูลด้วย ShapeCollection.add_table(), วางข้อความเซลล์ผ่าน TextFrame, ผสมเซลล์ และใช้การจัดรูปแบบขอบ. ความสูงแถวและคอลัมน์ ความกว้างตรงกับค่าที่คุณส่งไปยังผู้สร้าง.
from aspose.slides_foss import Presentation, FillType
from aspose.slides_foss.drawing import Color
table = slide.shapes.add_table(50, 50, [150, 150, 150], [40, 40, 40])
# Merge two cells in the second row
table.merge_cells(table.rows[1][0], table.rows[1][1], False)
# Style the header row with a bottom border
for col_idx in range(len(table.columns)):
fmt = table.rows[0][col_idx].cell_format
fmt.border_bottom.fill_format.fill_type = FillType.SOLID
fmt.border_bottom.fill_format.solid_fill_color.color = Color.from_argb(255, 0, 0, 0)
fmt.border_bottom.width = 2
ไซด์หลักและการจัดวางรูปภาพ
การนําเสนอทุกครั้งมีระดับการจัดสรรของภาพลักษณะหลักและรูปแบบ. เข้าถึงมัน ผ่าน Presentation.masters และ Presentation.layout_slides ใช้อย่างคงที่ การจัดรูปแบบผ่านเกาะของคุณ.
from aspose.slides_foss import Presentation
with Presentation() as pres:
master = pres.masters[0]
print("Master layouts:", len(master.layout_slides))
# Pick a layout by type
from aspose.slides_foss import SlideLayoutType
title_layout = pres.layout_slides.get_by_type(SlideLayoutType.TITLE)
if title_layout:
pres.slides.add_empty_slide(title_layout)
คุณสมบัติของเอกสาร
อ่านและเขียนเมตาเดทาที่ตั้งอยู่ในตัวอย่าง ชื่อเรื่อง ผู้แต่ง และหัวข้อผ่านการใช้งานของ DocumentProperties object. กําหนดสมบัติใด ๆ ก่อนการเรียก save() พวกเขาถูกนําไปใช้ใน metadata PPTX และสามารถอ่านได้โดยเครื่องมือด้านล่าง:
from aspose.slides_foss import Presentation
from aspose.slides_foss.export import SaveFormat
with Presentation() as pres:
props = pres.document_properties
props.title = "Quarterly Report"
props.author = "Engineering"
props.subject = "Q1 2026 Results"
pres.save("report.pptx", SaveFormat.PPTX)
เริ่มต้นอย่างรวดเร็ว
ใช้คําสั่ง pip ต่อไปเพื่อติดตั้ง Aspose.สไลด์ FOSS สําหรับ Python จาก PyPI:
pip install aspose-slides-foss
ตัวอย่างต่อไปนี้สร้างการแสดงภาพที่มีรูปแบบชื่อ, ตารางข้อมูล และการเติม แล้วบันทึกมันเป็น PPTX:
from aspose.slides_foss import Presentation, ShapeType, FillType, NullableBool
from aspose.slides_foss.drawing import Color
from aspose.slides_foss.export import SaveFormat
# Add a title shape
title = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 30, 860, 80)
tf = title.add_text_frame("Quarterly Report")
tf.paragraphs[0].portions[0].portion_format.font_bold = NullableBool.TRUE
tf.paragraphs[0].portions[0].portion_format.font_height = 28
# Add a data table
table = slide.shapes.add_table(50, 150, [200, 200, 200], [40, 40, 40])
headers = ["Region", "Revenue", "Growth"]
for i, h in enumerate(headers):
table.rows[0][i].text_frame.text = h
# Fill the title shape background
title.fill_format.fill_type = FillType.SOLID
title.fill_format.solid_fill_color.color = Color.from_argb(255, 0, 51, 102)
# Save as PPTX
pres.save("report.pptx", SaveFormat.PPTX)
รูปแบบที่สนับสนุน
| Format | Extension | Read | Write |
|---|---|---|---|
| PPTX | .pptx | ✓ | ✓ |
| PPT | .ppt | — | Planned |
| PPSX | .ppsx | — | Planned |
| PPTM | .pptm | — | Planned |
| POTX | .potx | — | Planned |
| ODP | .odp | — | Planned |
| .pdf การจัดสรร | — | Planned | |
| HTML | .html การใช้งาน | — | Planned |
| HTML5 | .html การใช้งาน | — | Planned |
| TIFF | .TIFF | — | Planned |
| GIF | .gif การใช้งาน | — | Planned |
| XPS | .xps | — | Planned |
| FODP | .fodp | — | Planned |
| MD | .md | — | Planned |
ข้อความ: PPTX อ่านและเขียน ได้ถูกนําไปใช้งานอย่างเต็มที่ SaveFormat ค่า (PPT, ODP, PDF เป็นต้น) มีแผนสําหรับการปล่อยในอนาคต; การส่งมันไปยัง save() ปัจจุบันผลิตการออก PPTX.
หลักสูตรและใบอนุญาต
Aspose.Slides FOSS for Python is released under the MIT license. You can use it in โครงการส่วนตัว วิทยาศาสตร์ และธุรกิจโดยไม่มีขั้นต่ํา. เอกสารแหล่งคือ มีในวันที่: GitHub และกระเป๋าสินค้าถูกประกาศในวันที่: PyPI.