Edit meta data with Pikepdf

1.5k views Asked by At

Anyone with experience editing pdf metadata with pikepdf?

I installed this library since other librarys seems to have some issues with table of contents and/or bookmarks. I also tried pdfrw, but this library couldn't overwrite existing values so, now I'm giving pikepdf a try.

Documentation says:

Open metadata in a with block to open it for editing. When the block is exited, changes are committed (updating XMP and the Document Info dictionary) and attached to the PDF object. The PDF must still be saved. If an exception occurs in the block, changes are discarded.

In [4]: with pdf.open_metadata() as meta:
   ...:     meta['dc:title'] = "Let's change the title"

I have tried this approach, using a function:

from pikepdf import Pdf

def add_metadata(source_pdf, author, title, out_dir):
    with Pdf.open(source_pdf) as pdf:
        with pdf.open_metadata() as meta:

            meta['dc:title'] = title
            meta['dc:creator'] = author
    
        pdf.save(os.path.join(out_dir, os.path.basename(source_pdf)))

After saving, nothing has changed. Am I missing something obvious here?

If I add a breakpoint in the function and try to check existing values, I get a no key found error. So I guess that is part of the problem - that I cant access the correct keys, but how can I see what key value pairs exists in this object that gets returned by open_metadata()? If I print the "meta" object as is, I get the following:

<x:xmpmeta xmlns:x="adobe:ns:meta/"
            x:xmptk="XMP toolkit 2.9.1-13, framework 1.6">
    <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
            xmlns:iX="http://ns.adobe.com/iX/1.0/">
        <rdf:Description xmlns:pdf="http://ns.adobe.com/pdf/1.3/"
                        rdf:about="Q"
                        pdf:Producer="GPL Ghostscript 8.70"/>
        <rdf:Description xmlns:xmp="http://ns.adobe.com/xap/1.0/"
                        rdf:about="W">
            <xmp:ModifyDate>2020-05-28T09:26:1Z</xmp:ModifyDate>
            <xmp:CreateDate>2020-05-28T09:26:1Z</xmp:CreateDate>
            <xmp:CreatorTool>XYZ</xmp:CreatorTool>
        </rdf:Description>
        <rdf:Description xmlns:xapMM="http://ns.adobe.com/xap/1.0/mm/"
                        rdf:about="E"
                        xapMM:DocumentID="R"/>
        <rdf:Description xmlns:dc="http://purl.org/dc/elements/1.1/"
                        rdf:about="T"
                        dc:format="application/pdf">
            <dc:title>
                <rdf:Alt>
                    <rdf:li xml:lang="x-default">Unknown</rdf:li>
                </rdf:Alt>
            </dc:title>
            <dc:creator>
                <rdf:Seq>
                    <rdf:li>user-id,S-D-F-G-12345678</rdf:li>
                </rdf:Seq>
            </dc:creator>
        </rdf:Description>
    </rdf:RDF>
</x:xmpmeta>

print(meta.keys()) --> KeysView(<pikepdf.models.metadata.PdfMetadata object at 0x000001E4024306A0>)

list(meta.keys()) --> []

Can someone point me in the right direction please? How do I change values "Unknown" and "user-id,S-D-F-G-12345678"?

1

There are 1 answers

0
Zug_Bug On

A workaround: Delete all metadata, and then add new ones.