我试图搜索作者的pdf文件,不允许使用任何第三方pdf模块

2024-10-03 11:16:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图搜索作者的pdf文件,不允许使用任何第三方pdf模块可用。我写的代码是以下内容:。我想做的是得到作者的名字,并认为使用re可以搜索字符串“author”并在字段中返回名字。我是Python编程新手,以前没有编程过。任何协助都将不胜感激。你知道吗

import re
f=bytes("k://file.pdf",'ascii')
open("k://file.pdf")


for line in f:
    if re.match("(.*)(Author)(.*)", line):
        print (line),
The error message I get is:

Traceback (most recent call last):
   File "K:\hw3pdftest.py", line 8, in <module>
    if re.match("(.*)(Author)(.*)", line):
   File "C:\Python34\lib\re.py", line 160, in match
    return _compile(pattern, flags).match(string)
TypeError: expected string or buffer

这是我首先尝试的,但得到以下错误消息:

Traceback (most recent call last):
  File "K:\hw3pdftest.py", line 6, in <module>
    for line in f:
  File "C:\Python34\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 515: character maps to <undefined>

然后我尝试使用cp1252对其进行编码,得到以下错误消息:

Traceback (most recent call last):
  File "K:\hw3pdftest.py", line 5, in <module>
    f=open("k://file.pdf", "r", "encodings=cp1252")
TypeError: an integer is required (got type str)

Tags: inpyremostpdfmatchlinecall
2条回答

这是不对的:

f = bytes("k://file.pdf",'ascii')
for line in f:
    ...

您不是迭代pdf中的行,而是迭代b'k://file.pdf'中的字节值,即字符k:/的ASCII码,这些字符是整数。你应该做:

f = open("k://file.pdf")
for line in f:
    ...

PDF将作者姓名(根据official PDF Specification)存储为PDF字典中的以下键:

/Author (John Doe)

因此,您应该尝试对PDF文件运行以下正则表达式

\/Author.+\((.+\))

它将返回匹配的作者名#1。请注意,在某些情况下,您可能需要对该字符串进行额外的解码(如果它使用Unicode符号,则可以通过特殊方式对其进行编码)

相关问题 更多 >