วิธีเพิ่มรูปทรงใน PowerPoint ด้วย Python

วิธีเพิ่มรูปทรงใน PowerPoint ด้วย Python

Aspose.Slides FOSS for Python รองรับการเพิ่ม AutoShapes, Tables, Connectors และ PictureFrames ไปยังสไลด์การนำเสนอ. รูปแบบรูปร่างทั้งหมดจะถูกเพิ่มผ่านคอลเลกชัน slide.shapes.

คู่มือแบบขั้นตอนต่อขั้นตอน

ขั้นตอนที่ 1: ติดตั้งแพคเกจ

pip install aspose-slides-foss

ตรวจสอบการติดตั้ง:

import aspose.slides_foss as slides
print("Ready")

ขั้นตอนที่ 2: สร้างงานนำเสนอ

ใช้ Presentation เป็นตัวจัดการบริบทเสมอ

import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]
    # ... add shapes ...
    prs.save("output.pptx", SaveFormat.PPTX)

ขั้นตอนที่ 3: เพิ่ม AutoShape

slide.shapes.add_auto_shape(shape_type, x, y, width, height) วางรูปทรงที่ตำแหน่งและขนาดที่กำหนด (ทั้งหมดเป็นจุด). ใช้ค่าคงที่ ShapeType เพื่อเลือกรูปทรง.

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]

    # Rectangle
    rect = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 300, 100)
    rect.add_text_frame("Rectangle shape")

    # Ellipse
    ellipse = slide.shapes.add_auto_shape(ShapeType.ELLIPSE, 400, 50, 200, 100)
    ellipse.add_text_frame("Ellipse shape")

    prs.save("autoshapes.pptx", SaveFormat.PPTX)

ขั้นตอนที่ 4: เพิ่มตาราง

slide.shapes.add_table(x, y, col_widths, row_heights) สร้างตารางที่ตำแหน่งที่ระบุ ความกว้างของคอลัมน์และความสูงของแถวเป็นรายการของค่าจุด.

import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]

    col_widths = [150.0, 150.0, 150.0]
    row_heights = [40.0, 40.0, 40.0]
    table = slide.shapes.add_table(50, 200, col_widths, row_heights)

    # Set header row text
    headers = ["Product", "Units", "Revenue"]
    for col, text in enumerate(headers):
        table.rows[0][col].text_frame.text = text

    # Set data rows
    rows = [
        ["Widget A", "120", "$2,400"],
        ["Widget B", "85", "$1,700"],
    ]
    for row_idx, row_data in enumerate(rows):
        for col, text in enumerate(row_data):
            table.rows[row_idx + 1][col].text_frame.text = text

    prs.save("table.pptx", SaveFormat.PPTX)

ขั้นตอนที่ 5: เพิ่มคอนเนคเตอร์

คอนเนคเตอร์เชื่อมต่อสองรูปทรงให้เห็นได้ชัดเจน สร้างรูปทรงก่อน แล้วจึงเพิ่มคอนเนคเตอร์และตั้งจุดเชื่อมต่อเริ่มต้นและสิ้นสุดของมัน.

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]

    box1 = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 100, 150, 60)
    box1.add_text_frame("Start")

    box2 = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 350, 100, 150, 60)
    box2.add_text_frame("End")

    conn = slide.shapes.add_connector(ShapeType.BENT_CONNECTOR3, 0, 0, 10, 10)
    conn.start_shape_connected_to = box1
    conn.start_shape_connection_site_index = 3  # right side of box1
    conn.end_shape_connected_to = box2
    conn.end_shape_connection_site_index = 1    # left side of box2

    prs.save("connector.pptx", SaveFormat.PPTX)

ดัชนีตำแหน่งการเชื่อมต่อจะถูกกำหนดเป็นเลข 0–3 สำหรับสี่เหลี่ยมผืนผ้า: ด้านบน=0, ด้านซ้าย=1, ด้านล่าง=2, ด้านขวา=3.


ขั้นตอนที่ 6: เพิ่มกรอบรูปภาพ

ฝังรูปภาพและเพิ่มลงในสไลด์เป็น PictureFrame. อ่านไบต์ของรูปภาพก่อน, เพิ่มลงในคอลเลกชันรูปภาพของงานนำเสนอ, แล้วสร้างเฟรม.

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    with open("logo.png", "rb") as f:
        image_data = f.read()

    image = prs.images.add_image(image_data)

    slide = prs.slides[0]
    slide.shapes.add_picture_frame(
        ShapeType.RECTANGLE,  # bounding shape type
        50, 50,               # x, y in points
        200, 150,             # width, height in points
        image
    )

    prs.save("with-image.pptx", SaveFormat.PPTX)

ปัญหาทั่วไปและการแก้ไข

รูปร่างปรากฏอยู่นอกพื้นที่สไลด์ที่มองเห็นได้

สไลด์มีขนาด 720 × 540 จุดโดยค่าเริ่มต้น. ค่าของ x หรือ y ที่เกินขอบเขตเหล่านี้จะทำให้รูปร่างอยู่นอกสไลด์. คงไว้ x < 720 และ y < 540, และตรวจสอบให้แน่ใจว่า x + width <= 720 และ y + height <= 540.

AttributeError: 'NoneType' object has no attribute 'text_frame'

add_auto_shape() คืนค่าอ็อบเจ็กต์รูปทรงโดยตรง หากคุณเห็น None ให้ตรวจสอบว่าคุณไม่ได้ละทิ้งค่าที่คืนกลับ.

ข้อความในเซลล์ตารางเป็นค่าว่างหลังจากการกำหนดค่า

คุณสมบัติที่ถูกต้องคือ .text_frame.text (ไม่ใช่ .text โดยตรงบนเซลล์) เข้าถึงเซลล์โดยใช้ table.rows[row_index][col_index].text_frame.text = "value".


คำถามที่พบบ่อย

ฉันสามารถเพิ่มรูปทรงได้กี่รูปในสไลด์?

ไม่มีขีดจำกัดที่กำหนดโดยไลบรารี. ขีดจำกัดเชิงปฏิบัติจะแปรตามขนาดไฟล์และความสามารถในการเรนเดอร์ของโปรแกรมดู PPTX เป้าหมายของคุณ.

ฉันสามารถเปลี่ยนตำแหน่งของรูปทรงหลังจากเพิ่มแล้วได้หรือไม่?

ใช่ วัตถุ shape ที่ส่งกลับโดย add_auto_shape() มีคุณสมบัติ x, y, width และ height ที่คุณสามารถตั้งค่าได้:

shape.x = 100
shape.y = 200
shape.width = 400
shape.height = 80

ฉันสามารถตั้งค่าสีเส้นขอบของรูปร่างได้หรือไม่?

ใช่, ผ่าน shape.line_format:

from aspose.slides_foss.drawing import Color
shape.line_format.fill_format.solid_fill_color.color = Color.from_argb(255, 200, 0, 0)

รองรับแผนภูมิหรือไม่?

ไม่มีแผนภูมิ, SmartArt, และอ็อบเจ็กต์ OLE ที่ดำเนินการในรุ่นนี้และทำให้เกิด NotImplementedError.


ดูเพิ่มเติม

 ภาษาไทย