PDF转换后文件打开很奇怪

时间:2017-01-20 00:14:44

标签: python pdf python-3.6

使用pdftotext终端命令(我运行Ubuntu 17.04)将文本从PDF文本剥离到文本(.txt)文件后,尝试从文件中读取,我得到了非常奇怪的输出。我用一个包含单个单词的txt测试了代码:t​​est。代码有效。它从文件中完美打印出来,告诉我这是pdftotext的错误或编码的错误。真正让我感到高兴的是,文件编辑器正常打开文件,并且所有测试都说文件是标准的UTF-8。这是我的PDF stripped text file,用:

创建
import tkinter
from tkinter.filedialog import askopenfilename
import subprocess
from subprocess import *
import os
from tkinter import Listbox
import sys
from tkinter import Scrollbar
from tkinter import Frame
from tkinter import Label
from tkinter import messagebox
from tkinter import Button

#win32, cygwin, linux, linux2, darwin

def chooser():
    returnedValue['filename'] = askopenfilename(**opts)
    try:
        subprocess.run(['pdftotext', returnedValue['filename'], '-raw'], check = True)
    except (TypeError, subprocess.CalledProcessError) as e:
        messagebox.showerror("Null error", "No file selected")
    if sys.platform == "linux" or sys.platform == "linux2" or sys.platform == "darwin":
        label = returnedValue['filename'].split('/')
        file2['file2'] = label[len(label)-1]
    else:
        label = returnedValue['filename'].split('\\')
        file2['file2'] = label[len(label)-1]

    L.insert(0, label[len(label)-1])

def trello():
    with open(returnedValue['filename'], 'r', encoding='utf8', errors='ignore') as f:
        content = f.readlines()
    content = [x.strip() for x in content]
    for line in content:
        print(line)


opts = {}
opts['filetypes'] = [('Portable Document Format','.pdf'),('all files','.*')]
opts['defaultextension'] = '.pdf'
opts['title'] = 'Select File'
opts['initialdir'] = [os.path.expanduser('~/')]

returnedValue = {}
returnedValue['filename'] = ""
file2 = {}
file2['file2'] = ""
root = tkinter.Tk()

B = Button(root, text = "Convert PDF", command = chooser)
B.pack()

F = Frame(root)

La = Label(F, text="PDFs Converted:")
La.pack()

S = Scrollbar(F)
S.pack(side=tkinter.RIGHT, fill=tkinter.Y)

L = Listbox(F, height=5, yscrollcommand=S.set)
L.pack()

S.config(command=L.yview)

F.pack()

Bue = Button(root, text="Send to Trello", command = trello)
Bue.pack()

root.geometry('{}x{}'.format(500, 500))
root.attributes("-topmost", True)
root.wm_title("PDF Chooser")
root.mainloop()

这是我在errors='ignore'函数中使用open()时出现的错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.6/tkinter/__init__.py", line 1702, in __call__
    return self.func(*args)
  File "./pdftotextCVM.py", line 34, in trello
    content = f.readlines()
  File "/usr/lib/python3.6/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 11:     invalid start byte

here是我使用errors=ignore时获得的。http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/config-idle-timeout.html。最后一次粘贴是我的Ubuntu终端历史可以追溯到的,所以可能会有更多,但你明白了。旁注:如果你不能说,我使用的是Python 3.6。

1 个答案:

答案 0 :(得分:1)

errors=ignore输出中向下滚动,超过二进制垃圾,我看到了:

22 0 obj
<<
/Type /Font
/Subtype /Type0
/BaseFont /OXSVSB+TimesNewRoman/Encoding /Identity-H
/ToUnicode 23 0 R
/DescendantFonts[ 24 0 R ]
>>

这些是PDF说明。似乎输入.pdf。正在打开文件,而不是输出.txt文件。

在打开文件之前,trello()函数可能需要将扩展​​名从.pdf更改为.txt