-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphaSwap.py
93 lines (81 loc) · 2.35 KB
/
alphaSwap.py
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
89
90
91
92
93
'''
Created by Aleksandar Damnjanovic
Date: 13.11.2021
Last edit: 22.11.2021
'''
from io import FileIO
import os
import docx2txt
import pdfplumber
import string
class alphaSwap:
def __init__(self) -> None:
self.d= self.parseCSV()
def parseCSV(self):
file= open('dictionary.csv', 'r')
content= file.read()
content=content.split('\n')
_d= dict()
for dd in content:
dd= dd.split(',')
_d[dd[0]]= dd[1]
return _d
def swapRawText(self, text):
letter=""
for c in text:
try:
letter+=str(self.d[c])
except:
letter+=str(c)
return letter
def swapTextFile(self, path):
file= open(path, "r")
text= file.read()
file.close()
t= self.swapRawText(text)
fname= os.path.basename(path)
nname= "swap_"+fname+".txt"
fpath= str.replace(path, fname,"")
npath=fpath+nname
file= open(npath,"w")
file.write(t)
file.close()
print("swap completed")
def swapDocxFile(self, path):
text= docx2txt.process(path)
t= self.swapRawText(text)
fname= os.path.basename(path)
nname= "swap_"+fname+".txt"
fpath= str.replace(path, fname,"")
npath=fpath+nname
file= open(npath,"w")
file.write(t)
file.close()
print("swap completed")
def swapPdfFile(self, path):
text= pdfplumber.open(path)
t=''
for p in text.pages:
t+= self.swapRawText(p.extract_text())
fname= os.path.basename(path)
nname= "swap_"+fname+".txt"
fpath= str.replace(path, fname,"")
npath=fpath+nname
file= open(npath,"w")
file.write(t)
file.close()
print("swap completed")
def processDirectory(self, path):
l=os.listdir(path)
d=''
for file in l:
if d=='':
d= str.replace(os.path.abspath(path), file, "")
if str.endswith(d,"/")!=True:
d+=os.sep
if (d+file).endswith(".pdf"):
self.swapPdfFile(d+file)
elif (d+file).endswith(".docx"):
self.swapDocxFile(d+file)
elif (d+file).endswith(".txt"):
self.swapTextFile(d+file)