使用Python通過簡單操作設置PDF文檔屬性
PDF文檔屬性是嵌入在PDF文檔中的一些與文檔有關的信息,如作者、制作軟件、標題、主題等。PDF屬性分為默認屬性和自定義屬性兩種,其中默認屬性是一些固定的文檔信息,部分信息自動生成(如文件大小、頁數(shù)、頁面大小等信息),其他信息則可以進行設置。設置這些文檔屬性可以幫助文檔閱讀者快速地掌握文檔的主要內(nèi)容以及關鍵信息,同時方便對文檔進行整理歸納。本文將介紹如何使用Python通過簡單的操作設置PDF文檔屬性。
- 設置PDF文檔默認屬性和自定義屬性
- 獲取PDF文檔默認屬性和自定義屬性
本文所使用的方法需要用到Spire.PDF for Python,一個PDF文檔處理API,可通過官網(wǎng)獲取或通過PyPI安裝:
pip install Spire.PDF
設置PDF文檔默認屬性和自定義屬性
使用 PdfDocument.DocumentInformation 屬性可以獲取PDF文檔的屬性信息(PdfDocumentInformation對象),接著使用 PdfDocumentInformation 類下的屬性以及PdfDocumentInformation.SetCustomProperty() 方法可對PDF文檔的默認屬性和自定義屬性進行設置。下面是示例:
- 創(chuàng)建 PdfDocument 對象并使用 PdfDocument.LoadFromFile() 方法加載PDF文檔。
- 通過 PdfDocument.DocumentInformation 屬性獲取該PDF文檔的屬性。
- 通過 PdfDocumentInformation 下的屬性設置PDF默認屬性。
- 使用 PdfDocumentInformation.SetCustomProperty() 方法設置PDF自定義屬性。
- 使用 PdfDocument.SaveToFile() 方法保存PDF文檔。
代碼示例
from spire.pdf import * from spire.pdf.common import * # 創(chuàng)建PdfDocument對象并載入PDF文檔 pdf = PdfDocument() pdf.LoadFromFile("示例.pdf") # 獲取PDF文檔屬性 properties = pdf.DocumentInformation # 設置PDF默認屬性 properties.Author = "喬林" properties.Creator = "PDF制作器" properties.Keywords = "數(shù)據(jù)庫;云數(shù)據(jù)庫" properties.Subject = "云數(shù)據(jù)庫及其優(yōu)勢介紹" properties.Title = "云數(shù)據(jù)庫的力量:強化數(shù)字化企業(yè)" properties.Producer = "PDF制作器 1.3" # 設置PDF自定義屬性 properties.SetCustomProperty("公司", "科技有限公司") properties.SetCustomProperty("標簽", "數(shù)據(jù)服務,數(shù)據(jù)儲存,云數(shù)據(jù)庫") # 保存PDF文檔 pdf.SaveToFile("PDF屬性.pdf") pdf.Close()
設置效果
獲取PDF文檔默認屬性和自定義屬性
獲取PDF文檔屬性也可以通過類似的操作。在獲取PDF文檔屬性信息后,直接通過 PdfDocumentInformation 類下的屬性獲取指定的默認屬性內(nèi)容,自定義屬性內(nèi)容則可以通過 PdfDocumentInformation.GetCustomProperty() 方法獲取。下面是示例:
- 創(chuàng)建 PdfDocument 對象并使用 PdfDocument.LoadFromFile() 方法加載PDF文檔。
- 通過 PdfDocument.DocumentInformation 屬性獲取該PDF文檔的屬性。
- 通過 PdfDocumentInformation 類下的屬性獲取特定屬性。
- 使用 PdfDocumentInformation.GetCustomProperty() 方法獲取特定的自定義屬性。
- 輸出屬性內(nèi)容。
代碼示例
from spire.pdf import * from spire.pdf.common import * # 創(chuàng)建PdfDocument對象并載入PDF文檔 pdf = PdfDocument() pdf.LoadFromFile("PDF屬性.pdf") # 獲取PDF文檔屬性 properties = pdf.DocumentInformation # 創(chuàng)建字符串 information = "" # 獲取默認屬性 information += "作者: " + properties.Author information += "\n標題: " + properties.Title information += "\n主題: " + properties.Subject information += "\n關鍵字: " + properties.Keywords information += "\n應用程序: " + properties.Creator information += "\nPDF 制作程序: " + properties.Producer # 獲取自定義屬性 information += "\n公司: " + properties.GetCustomProperty("公司") information += "\n標簽: " + properties.GetCustomProperty("標簽") # 輸出文檔屬性 print(information) pdf.Close()
輸出結(jié)果
以上就是使用Python通過簡單操作設置PDF文檔屬性的詳細內(nèi)容,更多關于Python設置PDF屬性的資料請關注腳本之家其它相關文章!
相關文章
控制Python浮點數(shù)輸出位數(shù)的操作方法
在python的輸出結(jié)果中,尤其是浮點數(shù)的輸出,當我們需要寫入文本文件時,最好是采用統(tǒng)一的輸出格式,這樣也能夠增強結(jié)果的可讀性,這篇文章主要介紹了控制Python浮點數(shù)輸出位數(shù)的方法,需要的朋友可以參考下2022-04-04使用Python和OpenCV實現(xiàn)動態(tài)背景的畫中畫效果
這篇文章將通過一個詳細的Python腳本,使用OpenCV庫來為視頻添加動態(tài)背景,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-11-11Python實戰(zhàn)之實現(xiàn)百度智能圖片識別
這篇文章主要介紹了如何利用Python編寫一個百度智能圖片識別項目,文中的示例代碼講解詳細,對我們學習Python有一定幫助,需要的可以參考一下2022-01-01Django用戶登錄與注冊系統(tǒng)的實現(xiàn)示例
這篇文章主要介紹了Django用戶登錄與注冊系統(tǒng)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06