在 Python 中建立流程圖 | Python 流程圖製作 API

流程圖 是流程的直覺說明。它使用一組符號(例如方框、菱形和箭頭)來示範所涉及的步驟以及每個階段所需做出的決策。

本文涵蓋以下主題:

Python 流程圖製作 API

Aspose.Diagram for Python 是一個函式庫,使程式設計師能夠直接在其 Python 應用程式中建立、編輯和操作 Visio 檔案。它提供了一組用於處理 Visio 檔案 的 API。我們將使用它在 Python 中以程式設計方式建立流程圖。

請在控制台中使用下列 pip 指令下載軟體包從 PyPI 安裝 API:

pip install aspose-diagram-python 

在 Python 中以程式設計方式建立流程圖

我們可以按照以下步驟輕鬆地用Python製作流程圖:

  1. 建立圖表的架構。
  2. 使用圖表類別載入用於新增形狀的母版。
  3. 使用 Shape 類別建立形狀。
  4. 使用 addshape() 方法將形狀新增至圖表。
  5. 使用 connectshapesviaconnector() 方法新增形狀連接器。
  6. 使用 LayoutOptions 類別設定圖表佈局。
  7. 之後,使用DiagramSaveOptions 類別指定儲存選項。
  8. 最後,使用 save() 方法將輸出檔案儲存為 VSDX 格式。

以下程式碼範例展示如何在 Python 中建立流程圖。

# 此程式碼範例示範如何在 Python 中建立流程圖。
import aspose.diagram
from aspose.diagram import *

def createFlowChart():
    # 要建立的圖表的架構
    diagram_object = Input(
        input_rectangles=[
            InputRectangle("A", "Manager"),
            InputRectangle("B", "Team Leader"),
            InputRectangle("C", "Team Member"),
            InputRectangle("D", "Team Member"),
            InputRectangle("E", "Team Member")
        ],
        input_connectors=[
            InputConnector("A", "B"),
            InputConnector("B", "C"),
            InputConnector("B", "D"),
            InputConnector("B", "E")
        ]
    )

    diagram = Diagram("D:\\Files\\BasicShapes.vss")
    page = diagram.pages[0]

    shape_names = {}

    # 從架構中新增形狀和連接器
   for rectangle in diagram_object.InputRectangles:
        shape = Shape()
        shape_id = diagram.add_shape(shape, "Rectangle", 0)
        shape_names[rectangle.Name] = shape_id
        shape = page.shapes.get_shape(shape_id)
        shape.text.value.add(Txt(rectangle.Text))

   for connector in diagram_object.InputConnectors:
        connector_id = diagram.add_shape(Shape(), "Dynamic connector", 0)
        page.connect_shapes_via_connector(
            shape_names[connector.OriginShapeName],
            aspose.diagram.manipulation.ConnectionPointPlace.RIGHT,
            shape_names[connector.DestinationShapeName],
            aspose.diagram.manipulation.ConnectionPointPlace.LEFT,
            connector_id
        )

    layout_options = aspose.diagram.autolayout.LayoutOptions()
    layout_options.layout_style = aspose.diagram.autolayout.LayoutStyle.FLOW_CHART
    layout_options.direction = aspose.diagram.autolayout.LayoutDirection.LEFT_TO_RIGHT
    layout_options.space_shapes = 5.0
    layout_options.enlarge_page = True
    

    diagram.layout(layout_options)

    page.page_sheet.print_props.print_page_orientation.value = PrintPageOrientationValue.LANDSCAPE

    save_options = aspose.diagram.saving.DiagramSaveOptions()
    save_options.save_format = SaveFileFormat.VSDX
    save_options.auto_fit_page_to_drawing_content = True

    diagram.save("D:\\Files\\flowchart_output.vsdx", save_options)
        
class Input:
    def __init__(self, input_rectangles=None, input_connectors=None):
        self.InputRectangles = input_rectangles if input_rectangles else []
        self.InputConnectors = input_connectors if input_connectors else []

class InputRectangle:
    def __init__(self, name, text):
        self.Name = name
        self.Text = text

class InputConnector:
    def __init__(self, origin_shape_name, destination_shape_name):
        self.OriginShapeName = origin_shape_name
        self.DestinationShapeName = destination_shape_name
        
createFlowChart()
在 Python 中以程式設計方式建立流程圖。

取得免費 API 許可證

您可以獲得免費的臨時許可證,以便在沒有評估限制的情況下使用 API。

Python 程式設計流程圖 – 免費資源

您可以使用以下資源了解有關流程圖 Python 程式設計的更多信息,並探索該庫的各種其他功能:

結論

在本文中,您學習如何在 Python 中以程式設計方式建立流程圖。透過利用Aspose.Diagram for Python,您可以使用不同類型的形狀(例如決策或流程)以及不同的佈局(例如從左到右或從右到左等)來建立各種類型的流程圖。如果有任何歧義,請透過我們的免費支援論壇聯絡我們。

也可以看看