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
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.