-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfFile.py
More file actions
executable file
·88 lines (64 loc) · 2.34 KB
/
pdfFile.py
File metadata and controls
executable file
·88 lines (64 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
import random
import string
import time
import subprocess
class pdfFile:
def __init__(self , caminho = None ):
if caminho != None:
self.atalizaPdf(caminho)
self.arquivoTxtCaminho()
self._info = None
self.arquivoSaidaTxt = None
self._arquivoTxtCaminho = None
diretorioAtual = os.getcwd()
os.system("cd " + diretorioAtual + " ; echo " " > pdftotext.log" )
def close(self ):
if self._info != None:
os.system("cd /tmp rm " + self._info )
def arquivoTxtCaminho(self):
if self._arquivoTxtCaminho == None:
self._arquivoTxtCaminho = self.getCaminhoAleatorio()
return self._arquivoTxtCaminho
def atalizaPdf( self , caminho ):
self.caminhoPdf = caminho
arq = open(caminho,"rb")
arqBytes = arq.read(5)
arq.close()
if "PDF" in arqBytes.decode("ascii"):
self.valido = True
else:
self.valido = False
def getText(self, atalizaPdf = None, cont = 0):
if atalizaPdf != None:
self.atalizaPdf(atalizaPdf)
if self.valido == True:
caminhoSaida = self.arquivoTxtCaminho()
comando = "pdftotext "+ atalizaPdf + " " + caminhoSaida + " 2>> pdftotext.log"
os.system( comando )
flag = False
for i in range( 0 , 101 ):
if os.path.isfile( caminhoSaida ):
flag = True
break
else:
if i % 10 == 0:
print( i, comando )
time.sleep(i/500)
if flag == True:
arq = open(caminhoSaida, "r")
text = arq.read()
arq.close()
os.system("rm " + self.arquivoTxtCaminho() )
else:
print("erro: log")
arq = open(caminhoSaida, "a")
arq.write( f"erro: {atalizaPdf}{caminhoSaida}")
arq.close()
return ""
return text
def getCaminhoAleatorio(self):
s = "/tmp/"
for i in range(0,15):
s += random.choice(string.ascii_letters)
return s