affinity icon indicating copy to clipboard operation
affinity copied to clipboard

I created xml library files for draw.io

Open mafalb opened this issue 4 years ago • 2 comments

However, I am relatively new to draw.io and I do not sure about the best/easiest/foolproof way to generate these xml files programmatically. For now I created the libraries by hand, which is suboptimal when a svg changes.

mafalb avatar Feb 16 '20 11:02 mafalb

I think this PR is quite usefull and should be merged

aldevar avatar May 13 '20 14:05 aldevar

I hacked something and it worked for me

import os
import base64
import json

folder_path = 'blue'
output_file = 'output.xml'
image_list = []

# Iterate through the files in the folder
for file_name in os.listdir(folder_path):
    if file_name.endswith('.svg'):  # Only process SVG files
        # Read the file content and encode it to base64
        with open(os.path.join(folder_path, file_name), 'rb') as f:
            encoded_content = base64.b64encode(f.read()).decode('utf-8')
        
        # Add the file data to the list
        image_list.append({
            'data': f'data:image/svg+xml;base64,{encoded_content}',
            'w': 50,
            'h': 50,
            'title': os.path.splitext(file_name)[0].replace("_blue",""),
            'aspect': 'fixed'
        })

# Output the XML format to a file
with open(output_file, 'w') as f:
    f.write('<mxlibrary>\n[\n')
    for i, image_data in enumerate(image_list):
        json_str = json.dumps(image_data, ensure_ascii=False)
        if i == len(image_list) - 1:
            f.write(f'\t{json_str}\n')
        else:
            f.write(f'\t{json_str},\n')
    f.write(']\n</mxlibrary>')
    

dominicpageau avatar Feb 09 '23 22:02 dominicpageau