【问题标题】:Writing metadata to a pdf using pyobjc使用 pyobjc 将元数据写入 pdf
【发布时间】:2010-11-04 19:21:49
【问题描述】:

我正在尝试使用以下 python 代码将元数据写入 pdf 文件:

from Foundation import *
from Quartz import *

url = NSURL.fileURLWithPath_("test.pdf")
pdfdoc = PDFDocument.alloc().initWithURL_(url)
assert pdfdoc, "failed to create document"

print "reading pdf file"

attrs = {}
attrs[PDFDocumentTitleAttribute] = "THIS IS THE TITLE"
attrs[PDFDocumentAuthorAttribute] = "A. Author and B. Author"

PDFDocumentTitleAttribute = "test"

pdfdoc.setDocumentAttributes_(attrs)
pdfdoc.writeToFile_("mynewfile.pdf")   

print "pdf made"

这似乎工作正常(控制台没有错误),但是当我检查文件的元数据时,如下所示:

PdfID0:
242b7e252f1d3fdd89b35751b3f72d3
PdfID1:
242b7e252f1d3fdd89b35751b3f72d3
NumberOfPages: 4

原始文件具有以下元数据:

InfoKey: Creator
InfoValue: PScript5.dll Version 5.2.2
InfoKey: Title
InfoValue: Microsoft Word - PROGRESS  ON  THE  GABION  HOUSE Compressed.doc
InfoKey: Producer
InfoValue: GPL Ghostscript 8.15
InfoKey: Author
InfoValue: PWK
InfoKey: ModDate
InfoValue: D:20101021193627-05'00'
InfoKey: CreationDate
InfoValue: D:20101008152350Z
PdfID0: d5fd6d3960122ba72117db6c4d46cefa
PdfID1: 24bade63285c641b11a8248ada9f19
NumberOfPages: 4

所以问题是,它不是附加元数据,而是清除以前的元数据结构。我需要做什么才能使其正常工作?我的目标是附加参考管理系统可以导入的元数据。

【问题讨论】:

    标签: python cocoa pdf pyobjc pdfkit


    【解决方案1】:

    Mark 走在了正确的轨道上,但有一些特点需要考虑。

    首先,他是正确的pdfdoc.documentAttributes 是一个包含文档元数据的NSDictionary。您想修改它,但请注意documentAttributes 为您提供NSDictionary,它是不可变的。您必须将其转换为NSMutableDictionary,如下所示:

    attrs = NSMutableDictionary.alloc().initWithDictionary_(pdfDoc.documentAttributes())
    

    现在您可以像以前一样修改attrs。没有必要像 Mark 建议的那样写 PDFDocument.PDFDocumentTitleAttribute,那是行不通的,PDFDocumentTitleAttribute 被声明为模块级常量,所以就像你在自己的代码中那样做。

    以下是适用于我的完整代码:

    from Foundation import *
    from Quartz import *
    
    url = NSURL.fileURLWithPath_("test.pdf")
    pdfdoc = PDFDocument.alloc().initWithURL_(url)
    
    attrs = NSMutableDictionary.alloc().initWithDictionary_(pdfdoc.documentAttributes())
    attrs[PDFDocumentTitleAttribute] = "THIS IS THE TITLE"
    attrs[PDFDocumentAuthorAttribute] = "A. Author and B. Author"
    
    pdfdoc.setDocumentAttributes_(attrs)
    pdfdoc.writeToFile_("mynewfile.pdf")
    

    【讨论】:

    • 感谢您的回答,塔玛斯。只有一个问题,我在哪里可以找到其他属性是什么?我正在尝试使用 PDF 对 .bib 文件中的元数据进行硬编码,但我不知道我可以存储的内容是否有任何限制。
    • 在苹果主页的PDFKit文档中查找即可;您正在寻找的常量列表在这里(对不起,长链接):developer.apple.com/library/mac/#documentation/GraphicsImaging/…。在其中查找“常量”部分和“文档属性键”。
    • 我在尝试这段代码时有点慢 - 但我无法让它超出 行。有没有关于 的特定版本?
    • 确切的错误消息:“pdfdoc = PDFDocument.alloc().initWithUrl_(url) AttributeError: 'PDFDocument' 对象没有属性 'initWithUrl_'”
    • Ermmm... 从我的 Python 终端复制源代码时,我发出了嘘声。它应该是initWithURL_ 而不是initWithUrl_。注意大写。
    【解决方案2】:

    免责声明:我对 Python 完全陌生,但我是 PDF 的老手。

    为避免破坏所有现有属性,您需要以pdfDoc.documentAttributes 开头attrs,而不是{}。 setDocumentAttributes 几乎可以肯定是 overwrite 而不是 merge (在此处给出您的输出)。

    其次,所有PDFDocument*Attribute 常量都是PDFDocument 的一部分。毫无疑问,我对 Python 的无知正在表现出来,但是您不应该将它们作为属性而不是作为裸变量来引用吗?像这样:

    attrs[PDFDocument.PDFDocumentTitleAttribute] = "THIS IS THE TITLE"
    

    你可以分配给 PDFDocumentTitleAttribute 让我相信它不是一个常数。

    如果我是对的,您的 attrs 将尝试为空键分配大量值。我的 Python 很弱,所以我不知道你会如何检查。在调用pdfDoc.setDocumentAttributes_() 之前检查attrs 应该会有所启发。

    【讨论】:

    • 感谢您的建议马克。我只是想了解您评论的第一部分 - 应该是 pdfDoc.documentAttributes = {} 还是 pdfDoc.documentAttributes.attrs = {}?
    • attrs = pdfdoc.documentAttributes
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多