Skip to content

How to add XMP metadata with PyPDF2? #492

@jvw1954

Description

@jvw1954

It is possible to add pdf-metadata to a PDF with PyPDF2:

...
Author = 'example'
Title = 'example'
Subject = 'example'
Creator = ''
Producer = ''
doc_print = {
             '/Author': Author,
             '/Title': Title,
             '/Subject': Subject,
             '/Creator': Creator,
             '/Producer': Producer,
             }
f = open(sys.argv[1], 'rb')
pdf = PdfFileReader(f)
output = PdfFileWriter()
output.addMetadata(doc_print)
#output.addMetadata(xmp_print)
output.appendPagesFromReader(pdf)
file = open(sys.argv[1] + ".new.pdf", "wb")
output.write(file)
file.close()
f.close()

The new metadata is then visible with PyPDF2 and Exiftool.

If I try to add xmp-metadata to a PDF no metadata is visible with PyPDF2 and Exiftool. What am I doing wrong?


...
xmp_print = {
             'dc:creator': Author,
             'dc:title': Title,
             'dc:subject': Subject,
             'dc:description': '',
             'dc:identifier': xmpmm_instanceId,
             'dc:publisher': '',
             'dc:coverage': 'NL',
             'dc:date': datetime.datetime(2019, 3, 15, 16, 47, 58).strftime("%Y-%m-%dT%H:%M:%SZ"),
             'dc:type': 'Text',
             'dc:relation': '',
             'dc:language': 'nl',
             'dc:source': 'id',
             'dc:format': 'application/pdf',
             'dc:contributor': 'name',
             'dc:rights': '',
             #'custom_properties': {},
             'pdf:keywords': 'keyword, keyword',
             'pdf:pdfversion': '',
             'pdf:producer': '',
             'xmpmm:documentId': xmpmm_documentId,
             'xmpmm:instanceId': xmpmm_instanceId,
             'xmp:createDate': xmp_createDate,
             'xmp:metadataDate': xmp_metadataDate,
             'xmp:modifyDate': dt,
             'xmp:creatorTool': 'PyPDF2',
             }
...
output.addMetadata(xmp_print)
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    is-questionRather a question than an issue. Should usually be a Discussion instead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions