在 Python 中转换 SVG 文件 – Python 示例

将 SVG 转换为其他格式可以更好地集成到需要 PDF(用于打印)或 PNG(用于 Web 使用)等格式的工作流程中。此外,转换为 JPEG 或 PNG 等光栅格式可确保跨设备和浏览器的一致渲染和显示。 Aspose.SVG for Python via .NET API 允许您将 SVG 文件转换为 PDFXPSJPGPNGBMPTIFFGIF 文件格式。您可以在 Python 项目中使用 API 来开发转换器应用程序,而无需了解底层文件格式的详细信息。

在线 SVG 转换器

使用Aspose.SVG,您可以将SVG实时转换为其他格式。只需从本地文件系统或 URL 加载 SVG,选择所需的输出格式,然后运行转换器即可。保存选项设置为默认值,您将立即收到作为单独文件的结果。

                
            

如何在 Python 中转换 SVG

您可以通过任何方式(在线或以编程方式)将 SVG 转换为各种流行格式。从 SVG 转换为其他格式可以通过使用 Converter 类的 convert_svg() 方法之一或 SVGDocument 类的 render_to(device) 方法来执行班级。

当前部分概述了使用ConverterSVGDocument类将 SVG 文件转换为其他流行格式的支持方案:

  1. 静态 Converter 类充当将 SVG 文件转换为流行格式的共享外观,使这些操作方便且直接。它提供了多种convert_svg()方法,可将 SVG 转换为 PDF、XPS 或图像文件。
  2. SVGDocument 类的 render_to() 方法用于将 SVG 渲染为另一种格式并将文档发送到输出设备。 Aspose.SVG for Python via .NET API 包括以下输出设备实现: PdfDeviceXpsDeviceImageDevice,它们有助于渲染为 PDF、XPS 和图像文件分别为格式。

让我们考虑将 SVG 文档转换为另一种文件格式的两种情况:

使用convert_svg()方法将 SVG 转换为 PNG

使用convert_svg()方法将 SVG 文件转换为另一种格式需要执行一系列步骤:

1from aspose.svg import SVGDocument
2from aspose.svg.converters import Converter
3from aspose.svg.saving import ImageSaveOptions
4
5options = ImageSaveOptions()
6with SVGDocument("image.svg") as document:
7
8	# Convert SVG to PNG
9	Converter.convert_svg(document, options, "image.png")

此示例演示如何使用默认转换选项将 SVG 文件转换为图像。如果您想控制转换过程并设置自己的保存选项,请参阅 将SVG转换为图像文章。

使用render_to()方法将 SVG 转换为 PDF

要使用render_to()方法将 SVG 转换为 PDF,请执行以下步骤:

 1import os
 2from aspose.svg import SVGDocument
 3from aspose.svg.rendering.pdf import PdfRenderingOptions, PdfDevice
 4
 5# Initialize an SVG document from a file
 6input_folder = "data/"
 7output_folder = "output/"
 8src_file = os.path.join(input_folder, "document.svg")
 9output_file = os.path.join(output_folder, "document.pdf")
10if not os.path.exists(output_folder):
11	os.makedirs(output_folder)
12
13with SVGDocument(src_file) as document:
14	# Initialize an instance of the PdfRenderingOptions class and set custom jpeg_quality properties
15	pdf_rendering_options = PdfRenderingOptions()
16	pdf_rendering_options.jpeg_quality = 10
17
18	# Initialize an instance of the PdfDevice class
19	with PdfDevice(pdf_rendering_options, output_file) as device:
20		# Render SVG to PDF and send the document to the rendering device
21		document.render_to(device)

常规选项 – RenderingOptions 类

RenderingOptions 类与特定设备类(例如 PdfDeviceXpsDeviceImageDevice)一起使用,表示渲染的 SVG 内容的目标输出格式。通过设置 RenderingOptions 类的属性,您可以确保渲染结果满足其特定需求和质量标准。这些选项允许您调整渲染的各个方面,例如页面设置、背景颜色以及不同输出格式的特定渲染选项。以下是 RenderingOptions 类的一些关键属性:

每个输出设备 PdfDeviceXpsDeviceImageDevice 都有自己独特的一组选项,分别通过类 PdfRenderingOptionsXpsRenderingOptionsImageRenderingOptions 实现。这些类继承了 RenderingOptions 类的基本属性,并且还具有自己的考虑格式功能的渲染属性。

Aspose.SVG 提供免费的在线 SVG 转换器,可让您轻松将 SVG 文件转换为各种流行格式。您只需选择文件、选择所需的格式并完成转换,即可将 SVG 转换为 PDF、XPS、JPG、PNG、BMP、TIFF 和 GIF。它快速、简单,而且完全免费!

文本“SVG 转换器”

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.