-1

我正在尝试从一个 txt 文件中的 3000 多个 PDF 中提取文本(而我必须从每一页中删除标题):

for x in range(len(files)-len(files)+15):
    pdfFileObj=open(files[x],'rb')
    pdfReader=PyPDF2.PdfFileReader(pdfFileObj)
    for pageNum in range(1,pdfReader.numPages):
        pageObj=pdfReader.getPage(pageNum)
        content=pageObj.extractText()
        removeIndex = content.find('information.') + len('information.')
        newContent=content[removeIndex:]
        file.write(newContent)
file.close()

但是,我收到以下错误:

return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\ufb02' in position 5217: character maps to <undefined>
4

1 回答 1

0

我无法检查每个 PDF 的编码,所以我只使用了 replace()。下面是工作代码:

for x in range(len(files)):
    pdfFileObj=open(os.path.join(filepath,files[x]),'rb')
    for pageNum in range(1,pdfReader.numPages):
        pageObj=pdfReader.getPage(pageNum)
        content=pageObj.extractText()
        removeIndex = content.find('information.') + len('information.')
        newContent=content[removeIndex:]
        newContent=newContent.replace('\n',' ')
        newContent=newContent.replace('\ufb02','FL')
        file.write(str(newContent.encode('utf-8')))
file.close()
于 2016-05-16T13:50:50.550 回答