dave-howard / vsdx

vsdx - A python library for processing .vsdx files
BSD 3-Clause "New" or "Revised" License
70 stars 25 forks source link

vsdx - A python library for processing Visio .vsdx files

PyPI PyPI - Downloads

pytest Documentation Status

PyPI - Python Version vsdx

.vsdx files can be processed in two ways, directly via python code as in example 1 below, or indirectly using a jinja template as in example 2

For quick start documentation please see https://vsdx.readthedocs.io/en/latest/quickstart.html

Example 1 code to find a shape with specific text, remove it, and then save the updated .vsdx file:

from vsdx import VisioFile

filename = 'my_file.vsdx'
# open a visio file
with VisioFile(filename) as vis:
  # find shape by its text on first page
  shape = vis.pages[0].find_shape_by_text('Shape to remove')
  # remove the shape if found
  if shape:
    shape.remove()
    # save a new copy
    vis.save_vsdx('shape_removed.vsdx')

Example 2 creating a new vsdx file from a template and context data using jinja.
Note that as vsdx does not lend itself well to ordered statements like {% if something %}my content{% endif %} or {% for x in list_value %}x={{ x }}{% endfor %} this package provides mechanisms to help - refer to tests for more details.

from vsdx import VisioFile

filename = 'my_template_file.vsdx'  # file containing jinja code
context = {'value1': 10, 'list_value': [1,2,3]}  # data for the template
with VisioFile('my_template_file.vsdx') as vis: 
    vis.jinja_render_vsdx(context=context)
    vis.save_vsdx('my_new_file.vsdx')

Please refer to tests/test.py for more usage examples in the form of pytest tests.


Change Log