Skip to content

Instantly share code, notes, and snippets.

@thiagoferreiraw
Last active November 25, 2021 19:12
Show Gist options
  • Save thiagoferreiraw/932afff89b5c88a8c8f943389401c12e to your computer and use it in GitHub Desktop.
Save thiagoferreiraw/932afff89b5c88a8c8f943389401c12e to your computer and use it in GitHub Desktop.
create_png_from_svg_template
import cairosvg
def create_png_from_svg_template(svg_path, replace_keys_dict, output_file):
"""
Create a png from a svg template, replacing keys in the svg with custom values.
Args:
svg_path (str): Path to the svg file
replace_keys_dict (dict): Keys to search/replace in the svg, e. g. {"code": 1}
output_file (file-like obj or str): file-like object or path
"""
with open(svg_path, "r") as f:
svg = "".join(f.readlines()).replace("\n", "")
for key, value in replace_keys_dict.items():
svg = svg.replace(key, str(value))
cairosvg.svg2png(svg, write_to=output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment