使用 Python 在簡報中建立線條形狀
Contents
[
Hide
]
概覽
Aspose.Slides for Python via .NET 支援在投影片中加入各種形狀。在本主題中,我們將透過在投影片中加入直線來開始使用形狀。使用 Aspose.Slides,開發人員不僅可以建立簡單的直線,還可以在投影片上繪製一些華麗的直線。
建立純直線
使用 Aspose.Slides 在投影片上加入純直線作為簡易分隔線或連接線。若要在簡報中選取的投影片上加入純直線,請遵循以下步驟:
- 建立 Presentation 類別的執行個體。
- 依照索引取得投影片的參考。
- 使用 ShapeCollection 物件的
add_auto_shape方法,加入類型為LINE的 AutoShape。 - 將簡報儲存為 PPTX 檔案。
下列範例會在簡報的第一張投影片加入一條直線。
import aspose.slides as slides
# 實例化 Presentation 類別。
with slides.Presentation() as presentation:
# 取得第一張投影片。
slide = presentation.slides[0]
# 新增類型為 LINE 的自動形狀。
slide.shapes.add_auto_shape(slides.ShapeType.LINE, 50, 150, 300, 0)
# 將簡報儲存為 PPTX 檔案。
presentation.save("line_shape.pptx", slides.export.SaveFormat.PPTX)
建立帶箭頭的直線
Aspose.Slides 允許您設定直線屬性,使其更具視覺吸引力。以下我們將設定直線的幾項屬性,使其呈現為箭頭。請遵循以下步驟:
- 建立 Presentation 類別的執行個體。
- 依照索引取得投影片的參考。
- 使用 ShapeCollection 物件的
add_auto_shape方法,加入類型為LINE的 AutoShape。 - 設定 line style。
- 設定直線寬度。
- 設定直線的 dash style。
- 設定直線起點的 arrowhead style 與長度。
- 設定直線終點的箭頭樣式與長度。
- 將簡報儲存為 PPTX 檔案。
import aspose.slides as slides
import aspose.pydrawing as draw
# 實例化代表 PPTX 檔案的 Presentation 類別。
with slides.Presentation() as presentation:
# 取得第一張投影片。
slide = presentation.slides[0]
# 新增類型為 LINE 的自動形狀。
shape = slide.shapes.add_auto_shape(slides.ShapeType.LINE, 50, 150, 300, 0)
# 套用線條的格式設定。
shape.line_format.style = slides.LineStyle.THICK_BETWEEN_THIN
shape.line_format.width = 10
shape.line_format.dash_style = slides.LineDashStyle.DASH_DOT
shape.line_format.begin_arrowhead_length = slides.LineArrowheadLength.SHORT
shape.line_format.begin_arrowhead_style = slides.LineArrowheadStyle.OVAL
shape.line_format.end_arrowhead_length = slides.LineArrowheadLength.LONG
shape.line_format.end_arrowhead_style = slides.LineArrowheadStyle.TRIANGLE
shape.line_format.fill_format.fill_type = slides.FillType.SOLID
shape.line_format.fill_format.solid_fill_color.color = draw.Color.maroon
# 將簡報儲存為 PPTX 檔案。
presentation.save("line_shape_2.pptx", slides.export.SaveFormat.PPTX)
常見問題
我可以將普通直線轉換為連接器,使其「貼齊」形狀嗎?
不會。普通直線(類型為 LINE 的 AutoShape)不會自動變成連接器。若要使其貼齊形狀,請使用專用的 Connector 類型以及用於連接的 corresponding APIs。
如果直線的屬性繼承自佈景主題且難以判斷最終值,我應該怎麼辦?
請透過 ILineFormatEffectiveData/ILineFillFormatEffectiveData 類別閱讀 有效屬性,這些類別已考慮繼承與佈景主題樣式。
我可以鎖定直線以防止編輯(移動、調整大小)嗎?
可以。形狀提供 lock objects,讓您 禁止編輯操作。