Microsoft® Visio 通过 Python 进行格式转换
将 MS Visio 图表转换为 HTML、PDF 和图像,包括 JPG、BMP、PNG、TIFF,以构建跨平台Python应用程序。
对于任何 Microsoft Visio 格式的渲染解决方案,例如设计流程图和业务流程图等 Python Visio API 以一种简单的方式促进所有复杂的绘图。使用加载源文件 Diagram 类 并使用适当的参数调用 save 方法。
相互转换 Visio 文件
程序员可以轻松转换 VSDX、VSX、VTX、VDX、VSSX、VSTX、VSDM、VSSM、VSTM 格式以及加载 VDW、VSD、 VSS、VST 并呈现为 PDF、HTML 和图像。考虑 VSDX 到 VDX 的场景,过程是,使用 diagram 类加载源 VSDX 文件并通过提供输出文件和调用 save 方法 保存文件格式 作为参数。
Python VSDX 到 VDX 转换的代码
// Load the VSDX in an object of Diagram | |
Diagram visio = new Diagram("template.vsdx"); | |
// save VSDX as VDX | |
visio.save("output.vdx", SaveFileFormat.VDX); |
Visio 到图像转换
对于通用转换,将 visio 文件转换为图像的过程是相同的。只需通过 Diagram 类加载文件并使用输出文件和 SaveFileFormat 输出参数调用 save 方法。并且每当需要定义特定选项时,开发人员可以在将 diagram 页面转换为图像时使用 ImageSaveOptions 类,并使用 SVGSaveOptions 进行 SVG 转换。
Python 将 Visio 转换为图像格式的代码
Diagram vsdxtoImages = new Diagram("sourceDocument.vsdx"); | |
ImageSaveOptions ImgOptions = new ImageSaveOptions(SaveFileFormat.JPEG); | |
// specify the quality level to use during compositing. | |
ImgOptions.setCompositingQuality(CompositingQuality.HIGH_QUALITY); | |
//The default value is 0.5. The value must be in the range between 0 and 1. | |
ImgOptions.setImageBrightness(1f); | |
ImgOptions.setDefaultFont("MS Gothic"); | |
// sets the number of pages to render in image. | |
ImgOptions.setPageCount(2); | |
// sets the 0-based index of the first page to render. Default is 0. | |
ImgOptions.setPageIndex(0); | |
// set page size | |
PageSize pgSize = new PageSize(PaperSizeFormat.A_1); | |
ImgOptions.setPageSize(pgSize); | |
// discard saving background pages of the Visio diagram | |
ImgOptions.setSaveForegroundPagesOnly(true); | |
// sets the color mode for the generated images. | |
ImgOptions.setImageColorMode(ImageColorMode.BLACK_AND_WHITE); | |
// the default value is 0.5. The value must be in the range between 0 and 1. | |
ImgOptions.setImageContrast(1f); | |
// this property has effect only when saving to raster image formats. | |
ImgOptions.setInterpolationMode(InterpolationMode.NEAREST_NEIGHBOR); | |
// the value may vary from 0 to 100 where 0 means worst quality, | |
ImgOptions.setJpegQuality(100); | |
// set a value specifying how pixels are offset during rendering. | |
ImgOptions.setPixelOffsetMode(PixelOffsetMode.HIGH_SPEED); | |
// sets the resolution for the generated images, in dots per inch. The default value is 96. | |
ImgOptions.setResolution(2f); | |
// the default value is 1.0. The value must be greater than 0. | |
ImgOptions.setScale(1f); | |
// specify whether smoothing (antialiasing) is applied to lines | |
// and curves and the edges of filled areas. | |
ImgOptions.setSmoothingMode(SmoothingMode.HIGH_QUALITY); | |
// sets the type of compression to apply when saving generated images to the TIFF format. | |
ImgOptions.setTiffCompression(TiffCompression.CCITT_3); | |
// save in visio file into image of choice | |
vsdxtoImages.save("visiofiletoimage_Out.jpeg", ImgOptions); |
Python 将 Visio 转换为 SVG 的代码
Diagram vsdxTosvg = new Diagram("sourceFile.vsdx"); | |
SVGSaveOptions svgOpts = new SVGSaveOptions(); | |
svgOpts.setDefaultFont("MS Gothic"); | |
// sets the 0-based index of the first page to render. Default is 0. | |
svgOpts.setPageIndex(0); | |
// set page size | |
PageSize pgSize = new PageSize(PaperSizeFormat.A_1); | |
svgOpts.setPageSize(pgSize); | |
vsdxTosvg.save("visio-to-svg.svg", svgOpts); |
将 Visio 转换为 PDF 和 HTML
API 能够将 visio 格式转换为 PDF 和 HTML。只需使用 保存文件格式 保存方法中的 .PDF 和 SaveFileFormat.HTML 作为参数。对于特殊设置,开发人员可以使用 PdfSaveOptions 和 HTMLSaveOptions 类。
Python Visio 到 PDF 转换的代码
// call the diagram constructor to load diagram from a VSDX file | |
Diagram vsdxtopdf = new Diagram("sourfile.vsdx"); | |
// Options when saving a diagram into the PDF format | |
PdfSaveOptions pdfOpts = new PdfSaveOptions(); | |
// discard saving background pages of the Visio diagram | |
pdfOpts.setSaveForegroundPagesOnly(true); | |
// specify the quality of JPEG compression for images (if JPEG compression is used). Default is 95. | |
pdfOpts.setJpegQuality(100); | |
// specify default font name | |
pdfOpts.setDefaultFont("Arial"); | |
// conformance level for generated PDF document. | |
pdfOpts.setCompliance(PdfCompliance.PDF_15); | |
// sets a digital signature details. If not set, then no signing will be performed. | |
pdfOpts.setDigitalSignatureDetails(new PdfDigitalSignatureDetails(cert, "Test Signing", "Aspose Office", DateTime.getNow(), PdfDigitalSignatureHashAlgorithm.SHA_256)); | |
// set encription details | |
PdfEncryptionDetails encriptionDetails = new PdfEncryptionDetails("user password", "Owner Password", PdfEncryptionAlgorithm.RC_4_128); | |
pdfOpts.setEncryptionDetails(encriptionDetails); | |
// sets the number of pages to render in PDF. | |
pdfOpts.setPageCount(2); | |
// sets the 0-based index of the first page to render. Default is 0. | |
pdfOpts.setPageIndex(0); | |
// set page size | |
PageSize pgSize = new PageSize(PaperSizeFormat.A_1); | |
pdfOpts.setPageSize(pgSize); | |
// save in visio file into PDF | |
vsdxtopdf.save("UsePDFSaveOptions_Out.pdf", pdfOpts); |
Python 将 Visio 转换为 HTML 文件的代码
// call the diagram constructor to load diagram from a VSDX file | |
Diagram vsdxtohtml = new Diagram("sourceFile.vsdx"); | |
// Options when saving a diagram into the HTML format | |
HTMLSaveOptions htmlOpt = new HTMLSaveOptions(); | |
htmlOpt.setDefaultFont("MS Gothic"); | |
// sets the number of pages to render in HTML. | |
htmlOpt.setPageCount(2); | |
// sets the 0-based index of the first page to render. Default is 0. | |
htmlOpt.setPageIndex(0); | |
// set page size | |
PageSize pgSize = new PageSize(PaperSizeFormat.A_1); | |
htmlOpt.setPageSize(pgSize); | |
// discard saving background pages of the Visio diagram | |
htmlOpt.setSaveForegroundPagesOnly(true); | |
// specify whether to include the toolbar or not. Default value is true. | |
htmlOpt.setSaveToolBar(false); | |
// set title of the HTML document | |
htmlOpt.setTitle("Title goes here"); | |
// save visio file in HTML file format | |
vsdxtohtml.save("UseHTMLSaveOptions_Out.html", htmlOpt); |