Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

programatically creating draw.io macros with python. Is there a guideline ?

Vilius Kukanauskas
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 11, 2024

Hello,

I was looking for a solution but couldn’t find it anywhere, so I’m posting my question here. If there’s an existing answer, please feel free to share the link!

I need to programmatically create draw.io diagrams. I’m retrieving data from PlantUML and converting it to Draw.io XML format. I can open the converted files in Draw.io in the browser, and everything works well.

Now, I need to edit existing pages and insert the Draw.io macro in specific locations.

From my understanding, I likely need to upload the Draw.io file first and then create a macro that references it ?

I can't seem to find a well-documented workflow for automating this process.

I’m using Python. Any help would be greatly appreciated!

2 answers

1 accepted

3 votes
Answer accepted
Vilius Kukanauskas
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 11, 2024

nwm, i found the solution. For those that were in search.

first upload the file as atachement. i have base_url in my config dict.

def upload_file(page_id, file_path):
"""Uploads a file to Confluence as an attachment to the specified page."""
base_url = cfg["site_url"] + "/wiki"

# Define the endpoint for uploading an attachment to the page
url = f"{base_url}/rest/api/content/{page_id}/child/attachment"

# Set the file data for upload
with open(file_path, 'rb') as file_data:
files = {'file': (os.path.basename(file_path), file_data, 'application/xml')}
response = requests.post(url, headers={"X-Atlassian-Token": "no-check"}, auth=get_auth(), files=files)



then create an html snippet for draw.io and use the file name in it. (file name, not the atachement id, without path, only filename)

def generate_drawio_html(file_name):
"""Generates HTML to embed a Draw.io diagram from an attached file."""
drawio_html = f"""
<ac:structured-macro ac:name="drawio">
<ac:parameter ac:name="diagramName">{file_name}</ac:parameter>
<ac:parameter ac:name="pageSize">false</ac:parameter>
<ac:parameter ac:name="attachment">{file_name}</ac:parameter>
</ac:structured-macro>
"""
return drawio_html

 then insert  the snipet in your html and update the confluence page with something like

response = requests.put(url, headers=get_headers(use_admin_cookie), auth=get_auth(), json=body)

solved, archive it for future searches :) 

0 votes
umashri
Contributor
July 8, 2025

Hey @Vilius Kukanauskas

I'm planning a migration from Confluence Data Center to Cloud and need to convert existing PlantUML macros to draw.io/diagrams.net format (since we won't be using the PlantUML plugin in Cloud).

Current Situation:

  • Hundreds of pages with PlantUML macros in Data Center

  • Need native draw.io XML format for Cloud compatibility

Key Questions:

  1. What would be the most reliable way to programmatically:

    • Extract PlantUML code from existing Confluence pages

    • Transform it into valid draw.io XML format

    • Re-insert as draw.io macros via API?

  2. Are there any existing tools/scripts (Python preferred) that handle this conversion?

  3. For complex diagrams, would you recommend:

    • Direct syntax conversion (PlantUML → draw.io XML)

    • Intermediate SVG generation + draw.io embedding

    • Another approach?

  4. Any special considerations when working with Confluence Cloud's draw.io macro format?

We're particularly interested in solutions that can be automated at scale. Any guidance or examples would be greatly appreciated!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events