以防萬一有人正在尋找一些工作代碼。 這是我使用的腳本。
import poppler
import sys
import urllib
import os
def main():
input_filename = sys.argv[1]
# http://blog.hartwork.org/?p=612
document = poppler.document_new_from_file('file://%s' % \
urllib.pathname2url(os.path.abspath(input_filename)), None)
n_pages = document.get_n_pages()
all_annots = 0
for i in range(n_pages):
page = document.get_page(i)
annot_mappings = page.get_annot_mapping()
num_annots = len(annot_mappings)
if num_annots > 0:
for annot_mapping in annot_mappings:
if annot_mapping.annot.get_annot_type().value_name != 'POPPLER_ANNOT_LINK':
all_annots += 1
print 'page: {0:3}, {1:10}, type: {2:10}, content: {3}'.format(i+1, annot_mapping.annot.get_modified(), annot_mapping.annot.get_annot_type().value_nick, annot_mapping.annot.get_contents())
if all_annots > 0:
print str(all_annots) + " annotation(s) found"
else:
print "no annotations found"
if __name__ == "__main__":
main()
雖然這可能是有用的,如果我想從PDF中提取所有文本,我只想提取註釋。 我之所以提到poppler的原因是因爲它的確提供了這個功能,很容易(http://cgit.freedesktop.org/poppler/poppler/tree/glib/poppler-annot.h)。 但是,我想用python。我找到了python-poppler綁定項目,但似乎並沒有提供對註釋的完全訪問。我的問題歸結爲「我做錯了還是圖書館不完整?」和「有沒有其他人提供相同的功能?」 – davidb 2009-07-10 13:54:08