I was surprised to see there is no code sample for PyPDF2 when the questions is explicitly asking for PyPDF2, so here it is:
from PyPDF2 import PdfFileReader, PdfFileWriter
fin = open('source.pdf', 'rb')
reader = PdfFileReader(fin)
writer = PdfFileWriter()
writer.appendPagesFromReader(reader)
metadata = reader.getDocumentInfo()
writer.addMetadata(metadata)
# Write your custom metadata here:
writer.addMetadata({
'/Some': 'Example'
})
fout = open('result.pdf', 'wb')
writer.write(fout)
fin.close()
fout.close()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…