‘’’
Author: zzx
Date: 2022-07-30 15:52:15
LastEditors: zdl
LastEditTime: 2022-07-30 16:03:20
FilePath: \matlabcodef:\BaiduNetdiskWorkspace\markdown写杂谈\python导出drawio文件
Description:
Copyright © 2022 by zdl, All Rights Reserved.
‘’’
需要的工具:vscode, draw.io扩展,draw.io桌面版 、python
提示:这个方法并没有简化流程,只是能够减少打开窗口,在vscode中直接查看原始文件,导出文件,效率并没有显著提升。
啰嗦的部分:
drawio流程图绘制软件比visio好用。而且vscode有插件。vs插件的优点在于支持mermaid流程图,缺点非常明显不支持指定分辨率图像导出。
网上检索发现drawio的桌面版可以在命令行操作。在drawio安装目录下运行cmd,然后通过draw.io -help 命令可以查看全部命令。
查看命令发现没有针对性的dpi设置。我就想到给图片输入宽度和高度的方式。根据我猜测这个宽度和高度应该对应的就是像素点数量,所以我就按照实际尺寸,以及dpi的定义
写了一个命令, 发现可用。但是每次都要计算宽度、高度、调用cmd命令窗口,太麻烦。功能强大的python加上插件齐全的vscode能不能全部实现呢?
折腾了几个小时,终于搞定了。
进入正题:
1、安装draw.io插件,和桌面版,记住桌面版的路径
2、绘制好的图片有一个实际的尺寸,记住实际尺寸的宽和高
3、python转换 宽度
dpivalue=600#dpi
realwidth=89.4#mm
realheight=81.2#mm
width=round(dpivalue*realwidth/25.4)
height=round(dpivalue*realheight/25.4)
4、 构造命令行,不嫌麻烦可以自己写完整目录
inputfilepath=r'F:\BaiduNetdiskWorkspace\00typora\大论文\drawio\算法流程图.drawio'
outputfilepath=r'F:\BaiduNetdiskWorkspace\00typora\大论文\drawio\test.png'
starttext=r'draw.io -x '
midletext=r" -f png -t --width "+str(width)+r" --height " +str(height)+r" -o "
commandtext1=starttext+inputfilepath+midletext+outputfilepath
5、 改变python文件工作目录并执行命令
path="D:/draw.io/"#安装文件夹
os.chdir( path )# 修改当前工作目录
v2=os.system(commandtext1)
代码量并不大,非常简单
'''
Author: zzx
Date: 2022-07-27 10:12:38
LastEditors: zdl
LastEditTime: 2022-07-30 16:10:22
FilePath: \matlabcodef:\BaiduNetdiskWorkspace\00typora\大论文\drawio\output.py
Description:
Copyright (c) 2022 by zdl, All Rights Reserved.
'''
import os
dpivalue=600#dpi
realwidth=89.4#mm
realheight=81.2#mm
width=round(dpivalue*realwidth/25.4)
height=round(dpivalue*realheight/25.4)
inputfilepath=r'F:\BaiduNetdiskWorkspace\00typora\大论文\drawio\算法流程图.drawio'
outputfilepath=r'F:\BaiduNetdiskWorkspace\00typora\大论文\drawio\test.png'
starttext=r'draw.io -x '
midletext=r" -f png -t --width "+str(width)+r" --height " +str(height)+r" -o "
commandtext1=starttext+inputfilepath+midletext+outputfilepath
path="D:/draw.io/"#安装文件夹
os.chdir( path )# 修改当前工作目录
v2=os.system(commandtext1)
# 快速运行F5
# print(v2)
#关于python权限的问题
# https://blog.csdn.net/qq_33731081/article/details/103812749
# 如何在python中运行命令行命令
# https://blog.csdn.net/qq_34769162/article/details/119037908
#报错为空值的问题
# https://blog.csdn.net/xiaoxiaogh/article/details/88320102
#关于drawio导出命令,灵感来源https://j2r2b.github.io/2019/08/06/drawio-cli.html
'''
Options:
-V, --version output the version number
-c, --create creates a new empty file if no file is
passed
-k, --check does not overwrite existing files
-x, --export export the input file/folder based on the
given options
-r, --recursive for a folder input, recursively convert
all files in sub-folders also
-o, --output <output file/folder> specify the output file/folder. If
omitted, the input file name is used for
output with the specified format as
extension
-f, --format <format> if output file name extension is
specified, this option is ignored (file
type is determined from output extension,
possible export formats are pdf, png, jpg,
svg, vsdx, and xml) (default: "pdf")
-q, --quality <quality> output image quality for JPEG (default:
90)
-t, --transparent set transparent background for PNG
-e, --embed-diagram includes a copy of the diagram (for PNG,
SVG and PDF formats only)
--embed-svg-images Embed Images in SVG file (for SVG format
only)
-b, --border <border> sets the border width around the diagram
(default: 0)
-s, --scale <scale> scales the diagram size
--width <width> fits the generated image/pdf into the
specified width, preserves aspect ratio.
--height <height> fits the generated image/pdf into the
specified height, preserves aspect ratio.
--crop crops PDF to diagram size
-a, --all-pages export all pages (for PDF format only)
-p, --page-index <pageIndex> selects a specific page, if not specified
and the format is an image, the first page
is selected
-g, --page-range <from>..<to> selects a page range (for PDF format only)
-u, --uncompressed Uncompressed XML output (for XML format
only)
--enable-plugins Enable Plugins
-h, --help display help for command
'''