• 通过Python设置及读取PDF属性,轻松管理PDF文档


    PDF文档属性是嵌入在PDF文档中的一些与文档有关的信息,如作者、制作软件、标题、主题等。PDF属性分为默认属性和自定义属性两种,其中默认属性是一些固定的文档信息,部分信息自动生成(如文件大小、页数、页面大小等信息),其他信息则可以进行设置。设置这些文档属性可以帮助文档阅读者快速地掌握文档的主要内容以及关键信息,同时方便对文档进行整理归纳。本文将介绍如何使用Python通过简单的操作设置PDF文档属性

    • 设置PDF文档默认属性和自定义属性
    • 获取PDF文档默认属性和自定义属性

    本文所使用的方法需要用到Spire.PDF for Python,一个PDF文档处理API,可通过官网获取或通过PyPI安装:

    pip install Spire.PDF
    
    • 1

    设置PDF文档默认属性和自定义属性

    使用 PdfDocument.DocumentInformation 属性可以获取PDF文档的属性信息(PdfDocumentInformation对象),接着使用 PdfDocumentInformation 类下的属性以及PdfDocumentInformation.SetCustomProperty() 方法可对PDF文档的默认属性和自定义属性进行设置。下面是示例:

    1. 创建 PdfDocument 对象并使用 PdfDocument.LoadFromFile() 方法加载PDF文档。
    2. 通过 PdfDocument.DocumentInformation 属性获取该PDF文档的属性。
    3. 通过 PdfDocumentInformation 下的属性设置PDF默认属性。
    4. 使用 PdfDocumentInformation.SetCustomProperty() 方法设置PDF自定义属性。
    5. 使用 PdfDocument.SaveToFile() 方法保存PDF文档。

    代码示例
    Python

    from spire.pdf import *
    from spire.pdf.common import *
    
    # 创建PdfDocument对象并载入PDF文档
    pdf = PdfDocument()
    pdf.LoadFromFile("示例.pdf")
    
    # 获取PDF文档属性
    properties = pdf.DocumentInformation
    
    # 设置PDF默认属性
    properties.Author = "乔林"
    properties.Creator = "PDF制作器"
    properties.Keywords = "数据库;云数据库"
    properties.Subject = "云数据库及其优势介绍"
    properties.Title = "云数据库的力量:强化数字化企业"
    properties.Producer = "PDF制作器 1.3"
    
    # 设置PDF自定义属性
    properties.SetCustomProperty("公司", "科技有限公司")
    properties.SetCustomProperty("标签", "数据服务,数据储存,云数据库")
    
    # 保存PDF文档
    pdf.SaveToFile("PDF属性.pdf")
    pdf.Close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    设置效果
    设置PDF文档默认属性和自定义属性

    获取PDF文档默认属性和自定义属性

    获取PDF文档属性也可以通过类似的操作。在获取PDF文档属性信息后,直接通过 PdfDocumentInformation 类下的属性获取指定的默认属性内容,自定义属性内容则可以通过 PdfDocumentInformation.GetCustomProperty() 方法获取。下面是示例:

    1. 创建 PdfDocument 对象并使用 PdfDocument.LoadFromFile() 方法加载PDF文档。
    2. 通过 PdfDocument.DocumentInformation 属性获取该PDF文档的属性。
    3. 通过 PdfDocumentInformation 类下的属性获取特定属性。
    4. 使用 PdfDocumentInformation.GetCustomProperty() 方法获取特定的自定义属性。
    5. 输出属性内容。

    代码示例
    Python

    from spire.pdf import *
    from spire.pdf.common import *
    
    # 创建PdfDocument对象并载入PDF文档
    pdf = PdfDocument()
    pdf.LoadFromFile("PDF属性.pdf")
    
    # 获取PDF文档属性
    properties = pdf.DocumentInformation
    
    # 创建字符串
    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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    输出结果
    获取PDF文档默认属性和自定义属性

    以上内容介绍了如何使用Python设置和获取PDF文档的默认属性和自定义属性,帮助文档管理及文档识别。Spire.PDF for Python还支持许多其他功能,前往Spire.PDF for Python教程查看更多内容。

  • 相关阅读:
    【Torch】torch.load( )系列语句解读解读,易学易用
    【React扩展】2、PureComponent、ErrorBoundary、render props和组件通信方式总结
    【RabbitMQ】RabbitMQ 集群的搭建 —— 基于 Docker 搭建 RabbitMQ 的普通集群,镜像集群以及仲裁队列
    C++函数模板
    Mac10.15.7上编译OpenJDK8u
    JuiceFS 新手必知 24 问
    [maven] 实现&使用 plugin 及 properties 简述
    【OpenCV实现图像:图像处理技巧之空间滤波】
    【AICFD案例教程】轴流风扇仿真分析
    java ssm企业员工健康管理系统#计算机毕业设计
  • 原文地址:https://blog.csdn.net/Eiceblue/article/details/134379244