0

With the help of Change metadata of pdf file with pypdf2 I worte the code below to add new metadata to a pdf-document, which runs perfectly. Nevertheless I cannot view the new Metadata when opening the details of my document. How can I view the new meta data in the details?

Note: I need to add the metadata "comments", which is readable for Elasticsearch

from PyPDF2 import PdfFileReader, PdfFileWriter

def editMeta(file, text):
    fin = open(file, 'rb')
    reader = PdfFileReader(fin)
    writer = PdfFileWriter()

    writer.appendPagesFromReader(reader)
    metadata = reader.getDocumentInfo()
    writer.addMetadata(metadata)

    writer.addMetadata({
        '/comments': text
    })

    fout = open(file, 'ab') 
    writer.write(fout)

    fin.close()
    fout.close()


if __name__ == "__main__":
    file = 'Test_Angebot.pdf'
    editMeta(file, '#cool')

read the metadata via python:

def get_info(file):
    with open(file, 'rb') as f:
        pdf = PdfFileReader(f)
        info = pdf.getDocumentInfo()
    print(info)
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
alipfi
  • 51
  • 1
  • 10
  • 1
    your code work fine in my case ..added 'comments' and later get info with the new metadata....maybe is your file pdf the problem – GiovaniSalazar Dec 03 '19 at 15:45
  • @GiovaniSalazar I can find the new added Metadata under "custom". But is there an actual way to change the field "comments"? – alipfi Dec 04 '19 at 09:31

0 Answers0