1

我正在尝试编辑TitlePDF 的元数据字段,以尽可能包含 ASCII 等价物。我正在使用 Python3 和模块pdfrw

如何进行替换元数据字段的字符串操作?

我的测试代码在这里:

from pdfrw import PdfReader, PdfWriter, PdfString
import unicodedata

def edit_title_metadata(inpdf):

    trailer = PdfReader(inpdf)

    # this statement is breaking pdfrw
    trailer.Info.Title = unicode_normalize(trailer.Info.Title)

    # also have tried:
    #trailer.Info.Title = PdfString(unicode_normalize(trailer.Info.Title))

    PdfWriter("test.pdf", trailer=trailer).write()
    return

def unicode_normalize(s):
    return unicodedata.normalize('NFKD', s).encode('ascii', 'ignore')

if __name__ == "__main__":

    edit_title_metadata('Anadon-2011-Scientific Opinion on the safety e.pdf')

回溯是:

Traceback (most recent call last):
  File "get_metadata.py", line 68, in <module>
    main()
  File "get_metadata.py", line 54, in main
    edit_title_metadata(pdf)
  File "get_metadata.py", line 11, in edit_title_metadata
    trailer.Info.Title = PdfString(unicode_normalize(trailer.Info.Title))
  File "get_metadata.py", line 18, in unicode_normalize
    return unicodedata.normalize('NFKD', s).encode('ascii', 'ignore')
  File "/path_to_python/python3.7/site-packages/pdfrw/objects/pdfstring.py", line 550, in encode
    if isinstance(source, uni_type):
TypeError: isinstance() arg 2 must be a type or tuple of types

笔记:

  • GitHub 上的这个问题可能与此有关。

  • FWIW,Python3.6也出现同样的错误

  • 我已经分享了 pdf(它有非 ascii 连字符,unicode char \u2010)

.

 wget https://gist.github.com/philshem/71507d4e8ecfabad252fbdf4d9f8bdd2/raw/cce346ab39dd6ecb3a718ad3f92c9f546761e87b/Anadon-2011-Scientific%2520Opinion%2520on%2520the%2520safety%2520e.pdf
4

1 回答 1

0

您必须.decode()在元数据字段上使用该方法:

trailer.Info.Title = unicode_normalize(trailer.Info.Title.decode())

和完整的工作代码:

from pdfrw import PdfReader, PdfWriter, PdfReader
import unicodedata

def edit_title_metadata(inpdf):

    trailer = PdfReader(inpdf)
    trailer.Info.Title = unicode_normalize(trailer.Info.Title.decode())
    PdfWriter("test.pdf", trailer=trailer).write()
    return

def unicode_normalize(s):
    return unicodedata.normalize('NFKD', s).encode('ascii', 'ignore')

if __name__ == "__main__":

    edit_title_metadata('Anadon-2011-Scientific Opinion on the safety e.pdf')
于 2019-05-21T07:42:43.007 回答