Change metadata of pdf file with pypdf2

前端 未结 4 1156
时光说笑
时光说笑 2020-12-14 05:06

I want to add a metadata key-value pair to the metadata of a pdf file.

I found a several years old answer, but I think this is way to complicated. I guess there is a

4条回答
  •  情话喂你
    2020-12-14 05:32

    Building on what Cyril N. stated, the code works fine, but it creates a lot of "trash" files since now you have the original file and the file with the metadata.

    I changed the code a bit since I will run this on hundreds of files a day, and don't want to deal with the additional clean-up:

    from PyPDF2 import PdfFileReader, PdfFileWriter
    
    fin = open('your_original.pdf', 'rb')
    reader = PdfFileReader(fin)
    writer = PdfFileWriter()
    
    writer.appendPagesFromReader(reader)
    metadata = reader.getDocumentInfo()
    writer.addMetadata(metadata)
    
    # Write your custom metadata here:
    writer.addMetadata({
        '/Title': 'this'
    })
    
    fout = open('your_original.pdf', 'ab') #ab is append binary; if you do wb, the file will append blank pages
    writer.write(fout)
    
    fin.close()
    fout.close()
    

    If you do want to have it as a new file, just use a different name for the pdf in fout and keep ab. If you use wb, you will append blank pages equal to your original file.

提交回复
热议问题