python - 使用 pypdf2 将元数据添加到 pdf

标签 python pdf metadata

Change metadata of pdf file with pypdf2的帮助下 我编写了下面的代码,将新的元数据添加到 pdf 文档中。运行代码时出现错误:

fin = open(file, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'file'

我已经尝试查找包含“os”包的文件 - 但同样的错误 想这里出了问题吗?该文件确实存在...

from PyPDF2 import PdfFileReader, PdfFileWriter

def editMeta(file, text):
    fin = open(file, 'rb')
    reader = PdfFileReader(fin)
    writer = PdfFileWriter()

    writer.appendPagesFromReader(reader)
    metadata = reader.getDocumentInfo()
    writer.addMetadata(metadata)

    writer.addMetadata({
        'comment': text
    })

    fout = open(file, 'ab') 
    writer.write(fout)

    fin.close()
    fout.close()


if __name__ == "__main__":
    file = 'Test_Angebot.pdf'
    editMeta('file', '#cool')

最佳答案

需要将'comment'更改为'/comment'

关于python - 使用 pypdf2 将元数据添加到 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59157494/

相关文章:

python - Scrapy 抛出 ImportError : cannot import name xmlrpc_client

java - 从现有的 Pdf 和 (HTML + CSS) 创建一个新的 Pdf

dependency-injection - 构建高度可配置的软件产品的最佳实践

objective-c - 使用 AVFoundation 修改音频元数据

java - 验证 (PDF/PKI) 签名的必要步骤

metadata - MPEG DASH 中的定时元数据?

python - 如何在 VSCode 中更改 Python 扩展语言环境?

Python,异步: decorator in class to simplify loop syntax

python - VIRTUALENV bash :/usr/local/bin/virtualenv:/usr/bin/python: bad interpreter: No such file or directory

c# - iTextSharp 表格宽度为页面的 100%