PostScript 檔案不像 DOCX 檔案或 PDF 檔案那樣常用,但它們在印刷出版領域仍然發揮著至關重要的作用。您可能會遇到需要透過插入文字來修改 PS 檔案的情況。作為 Python 開發人員,您可能會發現有多個程式庫可以透過程式設計方式完成此任務。然而,本指南將介紹最頂級的 Python SDK Aspose.Page for Python via .NET。因此,我們將講解如何使用 Python 在 PostScript 檔案中插入文本,並使此自動化操作更加方便開發人員。
PostScript SDK 安裝
在CMD中執行以下命令,安裝完成。就是這樣。
pip install aspose-page
或者,您可以從這裡下載 SDK 檔案。
透過 .NET 實現的 Python Aspose.Page 目前在 Windows 上受支持,並且即將推出對 macOS 和 Linux 的支援。
使用 Python 在 PostScript 檔案中插入文字 - 程式碼片段
本節將示範如何使用 Aspose.Page for Python via .NET 編輯 PostScript 檔案。該 Python SDK 對開發人員友好,並提供無縫的文件操作方法。
以下程式碼範例可讓您以程式設計方式開發 PostScript 檔案編輯器:
import aspose.page as page
from aspose.page import *
from aspose.page.eps import *
from aspose.page.eps.device import *
from aspose.page.font import *
import aspose.pydrawing
## Define the working directory and load the license.
data_dir = "data"
License = page.License();
License.set_license("License.lic");
# 為 PostScript 文件建立輸出流。
with open(data_dir + "AddText_outPS.ps", "wb") as out_ps_stream:
# 初始化 PsSaveOptions 類別的物件以建立 A4 大小的儲存選項。
options = PsSaveOptions()
# 要寫入 PS 檔案的文本
str = "ABCDEFGHIJKLMNO"
font_size: float = 48
# 透過建立 PsDocument 類別的物件來建立新的 1 頁 PS 文件。
document = PsDocument(out_ps_stream, options, False)
# 使用系統字體(位於系統字體資料夾中)填充文字
font = aspose.page.ExternalFontCache.create_font_by_family_name("Times New Roman", font_size, aspose.pydrawing.FontStyle.BOLD)
# 使用預設或已定義的顏色填滿文字。特定情況下為黑色。
document.fill_text(str, font, 50, 100)
# 用藍色填滿文字。
document.fill_text(str, font, 50, 150, aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue))
# 使用自訂字體(位於自訂字體資料夾中)填入文字。
dr_font = aspose.page.ExternalFontCache.fetch_dr_font("Palatino Linotype", font_size, aspose.pydrawing.FontStyle.REGULAR)
# 使用預設或已定義的顏色填滿文字。特定情況下為黑色。
document.fill_text(str, dr_font, 50, 200)
# 用藍色填滿文字。
document.fill_text(str, dr_font, 50, 250, aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue))
# 使用預設或已定義的 aspose.pydrawing.Pen 勾勒出文字輪廓。在特定情況下,它是黑色的,寬度為 1 磅的 aspose.pydrawing.Pen。
document.outline_text(str, font, 50, 300)
# 以藍紫色 2 點寬度 aspose.pydrawing.Pen 勾勒出文字輪廓。
pen = GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue_violet), 2)
document.outline_text(str, font, 50, 350, pen)
# 用橘色填滿文本,用 2 點寬度的藍色 aspose.pydrawing.Pen 描邊。
document.fill_and_stroke_text(str, font, 50, 400, aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.yellow),
GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue_violet), 2))
# 使用預設或已定義的 aspose.pydrawing.Pen 勾勒出文字輪廓。在特定情況下,它是黑色的,寬度為 1 磅的 aspose.pydrawing.Pen。
document.outline_text(str, dr_font, 50, 450)
# 以藍紫色 2 點寬度 aspose.pydrawing.Pen 勾勒出文字輪廓。
document.outline_text(str, dr_font, 50, 500,
GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue_violet), 2))
#F ill text with orange color and stroke with blue colored 2-points width aspose.pydrawing.Pen.
document.fill_and_stroke_text(str, dr_font, 50, 550, aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.orange),
GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue), 2))
#Close current page
document.close_page()
#Save the document by calling the save method.
document.save()
輸出:

取得免費許可證
取得免費臨時許可證以試用此 SDK,不受評估限制。
提出問題
請造訪我們的論壇提出問題。
總結
顯然,處理 PS 檔案似乎有些棘手,但透過選擇合適的程式解決方案可以實現自動化。在這篇文章中,我們介紹如何使用 Python SDK 在 PostScript 檔案中插入文字。 Aspose.Page for Python via .NET 透過提供實用的類別和方法,讓 PS 檔案操作變得輕而易舉。因此,請務必造訪 文件 和 API 參考 以了解更多資訊。
查看我們的 GitHub 範例
請造訪我們的 GitHub 查看我們的實作。
常見問題解答
什麼是 PostScript 格式?
PostScript (PS) 是由 Adobe 開發的頁面描述語言,用於描述如何顯示或列印文字、圖像和圖形。
如何在 Python 中為 PostScript 檔案添加文字?
透過 .NET 安裝 Aspose.Page for Python 並編寫幾行原始程式碼,即可在 PostScript 檔案中插入文字。請造訪此 link 以取得完整答案。
如何在 Python 中編輯 PostScript 檔案?
建立 PsDocument 類別的實例並載入 PostScript 文件,然後透過其繪圖方法新增或修改內容(例如文字、圖片或圖形)。最後,使用 save() 方法儲存更新的檔案。