最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

shapes - How to Generate a Flowchart with Swimlanes and Export to Visio Using Python? - Stack Overflow

programmeradmin3浏览0评论

Title: Why aren't shapes appearing in my generated Visio file using the vsdx Python library?

Description:

I'm working with the vsdx Python library to generate a flowchart and export it to Visio. My goal is to add new shapes to the Visio file and update existing ones. I've attempted the following:

  1. Opened an existing Visio file (Flowchart_Walmart.vsdx).
  2. Updated the text of an existing shape using find_shape_by_text.
  3. Created and added a new shape with XML using Element and SubElement.

However, the shapes I'm trying to add are not visible in the generated Visio file. Here's the code I used:

filename = "Flowchart_Walmart.vsdx"
new_file = "diagram_with_shapes.vsdx"

# Open the copied Visio file
with VisioFile(filename) as v:
    page = v.pages[0]  # Get the first page
    
    # Find an existing shape and update its text
    existing_shape = page.find_shape_by_text(text="Is the customer new or existing?")
    if existing_shape:
        existing_shape.text = "Updated: Is the customer new or existing?"
        existing_shape.shape_type = "Process"

    # Create a new shape
    shape_xml = Element("Shape", {"ID": "100", "name": "Process", "type": "Shape"})  # Ensure unique ID
    text_element = SubElement(shape_xml, "Text")
    text_element.text = "New Shape Added"

    # Convert to VisioShape and add to page
    new_shape = Shape(xml=shape_xml, parent=page, page=page)
    page.shapes.append(new_shape)

    # Save the updated Visio file
    v.save_vsdx(new_file)

When I open the generated file, the new shape does not appear on the Visio diagram. The updated shape text also doesn't reflect the changes.

Here are the issues I suspect:

  • The new shape may not be properly added to the shapes list or rendered on the page.
  • The XML structure for the new shape might be missing required elements or attributes.
  • There could be additional steps needed for the vsdx library to register and render new shapes.

Could someone please help me understand:

  1. Why the new shape is not showing up in the generated file?
  2. If additional steps are required to properly add and render shapes in Visio using the vsdx library?
  3. Are there better alternatives or workarounds for creating flowcharts in Python and exporting them to Visio?

I am trying to generate a Visio file that contains flowchart shapes like Process, Decision, etc., along with the connections between nodes. However, the output I generated is a plain Visio file that does not contain any shapes. Can anyone please provide a solution or suggest corrections to my current approach?

Title: Why aren't shapes appearing in my generated Visio file using the vsdx Python library?

Description:

I'm working with the vsdx Python library to generate a flowchart and export it to Visio. My goal is to add new shapes to the Visio file and update existing ones. I've attempted the following:

  1. Opened an existing Visio file (Flowchart_Walmart.vsdx).
  2. Updated the text of an existing shape using find_shape_by_text.
  3. Created and added a new shape with XML using Element and SubElement.

However, the shapes I'm trying to add are not visible in the generated Visio file. Here's the code I used:

filename = "Flowchart_Walmart.vsdx"
new_file = "diagram_with_shapes.vsdx"

# Open the copied Visio file
with VisioFile(filename) as v:
    page = v.pages[0]  # Get the first page
    
    # Find an existing shape and update its text
    existing_shape = page.find_shape_by_text(text="Is the customer new or existing?")
    if existing_shape:
        existing_shape.text = "Updated: Is the customer new or existing?"
        existing_shape.shape_type = "Process"

    # Create a new shape
    shape_xml = Element("Shape", {"ID": "100", "name": "Process", "type": "Shape"})  # Ensure unique ID
    text_element = SubElement(shape_xml, "Text")
    text_element.text = "New Shape Added"

    # Convert to VisioShape and add to page
    new_shape = Shape(xml=shape_xml, parent=page, page=page)
    page.shapes.append(new_shape)

    # Save the updated Visio file
    v.save_vsdx(new_file)

When I open the generated file, the new shape does not appear on the Visio diagram. The updated shape text also doesn't reflect the changes.

Here are the issues I suspect:

  • The new shape may not be properly added to the shapes list or rendered on the page.
  • The XML structure for the new shape might be missing required elements or attributes.
  • There could be additional steps needed for the vsdx library to register and render new shapes.

Could someone please help me understand:

  1. Why the new shape is not showing up in the generated file?
  2. If additional steps are required to properly add and render shapes in Visio using the vsdx library?
  3. Are there better alternatives or workarounds for creating flowcharts in Python and exporting them to Visio?

I am trying to generate a Visio file that contains flowchart shapes like Process, Decision, etc., along with the connections between nodes. However, the output I generated is a plain Visio file that does not contain any shapes. Can anyone please provide a solution or suggest corrections to my current approach?

Share Improve this question asked Mar 19 at 10:37 sayen vvsayen vv 111 bronze badge 1
  • Are you using the free/trial version or the licensed version of the library? It seems the trial version has certain features disabled. I might be wrong here as well. – Naveed Ahmed Commented Mar 19 at 10:58
Add a comment  | 

1 Answer 1

Reset to default 0
Element("Shape", {"ID": "100", "name": "Process", "type": "Shape"}

Where is this shape coming from? Normally with Visio you need to have a stencil open and reference a shape master within that stencil.

On top of that a shape that has been added to a page needs to have a height and width, as well as X and Y coordinates.

Also you have no line style, the shape might be being created but invisible against the background.

发布评论

评论列表(0)

  1. 暂无评论