【问题标题】:Change metadata of pdf file with pypdf使用 pypdf 更改 pdf 文件的元数据
【发布时间】:2010-04-04 14:19:58
【问题描述】:

我想使用 pypdf 创建/修改 pdf 文档的标题。似乎标题是只读的。有没有办法以 r/w 方式访问此元数据?

如果回答是肯定的,我们将不胜感激。

谢谢

【问题讨论】:

    标签: pdf metadata pypdf


    【解决方案1】:

    您可以使用 pyPDF(有点)来操作标题。我在 reportlab-users 列表中看到了这篇文章:

    http://two.pairlist.net/pipermail/reportlab-users/2009-November/009033.html

    您也可以使用 pypdf。 http://pybrary.net/pyPdf/

    这不会让您编辑元数据 本身,但会让你读一个或 更多pdf文件并将它们吐回 出来,可能带有新的元数据。

    以下是相关代码:

    from pyPdf import PdfFileWriter, PdfFileReader
    from pyPdf.generic import NameObject, createStringObject
    
    OUTPUT = 'output.pdf'
    INPUTS = ['test1.pdf', 'test2.pdf', 'test3.pdf']
    
    # There is no interface through pyPDF with which to set this other then getting
    # your hands dirty like so:
    infoDict = output._info.getObject()
    infoDict.update({
        NameObject('/Title'): createStringObject(u'title'),
        NameObject('/Author'): createStringObject(u'author'),
        NameObject('/Subject'): createStringObject(u'subject'),
        NameObject('/Creator'): createStringObject(u'a script')
    })
    
    inputs = [PdfFileReader(i) for i in INPUTS]
    for input in inputs:
        for page in range(input.getNumPages()):
            output.addPage(input.getPage(page))
    
    outputStream = file(OUTPUT, 'wb')
    output.write(outputStream)
    outputStream.close()
    

    【讨论】:

    • 在构造 PdfFileReader 时,需要传递类似文件的对象,而不是字符串/文件名(至少使用 pyPdf 1.13)
    • PyPDF2(似乎已经取代了 pyPDF)有一个本地方法可以为您执行此操作:output.addMetadata({'/Title': 'title'})
    猜你喜欢
    • 2018-04-01
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2013-06-10
    • 2021-04-29
    相关资源
    最近更新 更多