Unless you pay for a PDF writing application then it can seem difficult if you want to edit or add to PDF file properties. Adobe Acrobat Reader for example lets you view document properties (open the document then click File – Properties) but you need to have Acrobat Pro to be able to edit properties.
Fortunately it’s simple to add or amend PDF properties with Python and PyPDF2. The script below will help you edit and add PDF properties.
from PyPDF2 import PdfFileReader, PdfFileWriter
filein = open('C:/Users/username/Downloads/old_file.pdf', 'rb')
reader = PdfFileReader(filein)
writer = PdfFileWriter()
writer.appendPagesFromReader(reader)
metadata = reader.getDocumentInfo()
writer.addMetadata(metadata)
# Add your properties here:
writer.addMetadata({
'/Author': 'Your Name',
'/Keywords':'your,keywords',
'/Subject':'Your subject',
'/Title':'Enter the document title'
})
fileout = open('C:/Users/username/Downloads/new_file.pdf', 'wb')
writer.write(fileout)
filein.close()
fileout.close()
I found this code online however am unable to locate the original source.
Leave a ReplyCancel reply