In [1]:
import PyPDF2

In [2]:
src_pdf = PyPDF2.PdfFileReader('data/src/pdf/sample1.pdf')
dst_pdf = PyPDF2.PdfFileWriter()

In [3]:
dst_pdf.cloneReaderDocumentRoot(src_pdf)

In [4]:
d = {key: src_pdf.documentInfo[key] for key in src_pdf.documentInfo.keys()}

In [5]:
d['/Title'] = 'new title'
d['/Author'] = 'new author'
d['/XXX'] = 'special data'

In [6]:
dst_pdf.addMetadata(d)

In [7]:
with open('data/temp/sample1_new_meta.pdf', 'wb') as f:
    dst_pdf.write(f)

In [8]:
print(PyPDF2.PdfFileReader('data/temp/sample1_new_meta.pdf').documentInfo)


{'/Producer': 'macOS バージョン10.14.2(ビルド18C54) Quartz PDFContext', '/Title': 'new title', '/Creator': 'Keynote', '/CreationDate': "D:20190114072947Z00'00'", '/ModDate': "D:20190114072947Z00'00'", '/Author': 'new author', '/XXX': 'special data'}

In [9]:
def update_metadata(src_path, dst_path, metadata):
    src_pdf = PyPDF2.PdfFileReader(src_path)
    dst_pdf = PyPDF2.PdfFileWriter()
    dst_pdf.cloneReaderDocumentRoot(src_pdf)
    
    d = {key: src_pdf.documentInfo[key] for key in src_pdf.documentInfo.keys()}
    d.update(metadata)

    dst_pdf.addMetadata(d)
    with open(dst_path, 'wb') as f:
        dst_pdf.write(f)

In [10]:
update_metadata('data/src/pdf/sample1.pdf', 'data/temp/sample1_new_meta.pdf',
                {'/Title': 'new title', '/Author': 'new author', '/Producer': ''})
print(PyPDF2.PdfFileReader('data/temp/sample1_new_meta.pdf').documentInfo)


{'/Producer': '', '/Title': 'new title', '/Creator': 'Keynote', '/CreationDate': "D:20190114072947Z00'00'", '/ModDate': "D:20190114072947Z00'00'", '/Author': 'new author'}

In [11]:
def set_metadata(src_path, dst_path, metadata):
    src_pdf = PyPDF2.PdfFileReader(src_path)
    dst_pdf = PyPDF2.PdfFileWriter()
    dst_pdf.cloneReaderDocumentRoot(src_pdf)
    dst_pdf.addMetadata(metadata)
    with open(dst_path, 'wb') as f:
        dst_pdf.write(f)

In [12]:
set_metadata('data/src/pdf/sample1.pdf', 'data/temp/sample1_new_meta.pdf',
             {'/Title': 'new title', '/Author': 'new author', '/Producer': ''})
print(PyPDF2.PdfFileReader('data/temp/sample1_new_meta.pdf').documentInfo)


{'/Producer': '', '/Title': 'new title', '/Author': 'new author'}