本文重点介绍如何在 Python 中创建 EPS 文件。它解释了在 Python 中创建 EPS 文件的分步算法和示例代码。此外,它还介绍了自定义属性,以便根据您的需要改进应用程序。
使用 Python 创建 EPS 文件的步骤
- 通过安装用于创建 PostScript 文件的 Aspose.Page 来设置系统
- 创建 PsSaveOptions 类的对象并设置不同的属性
- 创建 PsDocument 类实例
- 关闭并保存输出 EPS 文件
这些步骤概述了如何在 Python 中制作 EPS 文件。创建一个输出流以写入生成的 PostScript 文件,指定所需的自定义属性,并根据您的应用程序模型将其渲染到流或磁盘。此外,您不需要熟悉页面描述语言,因为只需调用几个 API 即可顺利完成任务。
使用 Python 制作 EPS 文件的代码
import aspose.page | |
from aspose.page import * | |
from aspose.page.eps.device import * | |
from aspose.page.eps import * | |
import io | |
#Create output stream for PostScript document | |
with open("CreateEPS.ps", "wb") as out_ps_stream: | |
#Create save options | |
options = PsSaveOptions() | |
#If you want to aassign page size other than A4, set page size in options | |
options.page_size = PageConstants.get_size(PageConstants.SIZE_A4, PageConstants.ORIENTATION_PORTRAIT) | |
#If you want to aassign page margins other empty, set page margins in options | |
options.margins = PageConstants.get_margins(PageConstants.MARGINS_ZERO) | |
#Set variable that indicates if resulting PostScript document will be multipaged | |
multi_paged = False | |
# Create new PS Document with one page opened | |
document = PsDocument(out_ps_stream, options, multi_paged) | |
#Close current page | |
document.close_page() | |
#Save the document | |
document.save() |
此代码片段演示了使用 Python 创建 EPS 的快速方法。首先,创建 PsSaveOptions 类实例,您可以在其中自定义不同的属性,如背景颜色、透明度、图像质量、页面大小、边距等。随后,启动 PsDocument 类的对象,并呈现输出 PostScript 文件以完成该过程。
本指南帮助您了解如何在 Python 中创建 EPS 文件。但是,如果您想将 EPS 渲染为图像,请查看 在 Python 中将 EPS 转换为图像 上的文章。