-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTML-Beauti.py
More file actions
130 lines (96 loc) · 3.84 KB
/
Copy pathHTML-Beauti.py
File metadata and controls
130 lines (96 loc) · 3.84 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from bs4 import BeautifulSoup
import os, glob, sys
def file_replace_text(fname, toreplace, replacement):
#print("opening " + fname)
with open(fname, 'r', encoding='utf-8') as file:
filedata = file.read()
# Replace the text in file
filedata = filedata.replace(toreplace, replacement)
# Write the file out again
with open(fname, 'w', encoding='utf-8') as file:
file.write(filedata)
def main():
if len(sys.argv) <= 1:
print("No parameters detected. Aborting.")
input("Press Enter to continue...")
elif len(sys.argv) == 1:
#print("ARG detected: "+sys.argv[1])
kbarg = sys.argv[1]
else:
kbarg = sys.argv[1:]
#dirname = os.path.dirname(__file__)
dirname = '\\\\fs-hsg-1\\IT Department\\KB PDF2HTML\\Organized'
# print(dirname)
for KBID in kbarg:
print('Beauti.pying ' + KBID, end=" ")
folderpath = os.path.join(dirname, KBID)
# print(folderpath)
if not (os.path.isdir(folderpath)):
print("KBID doesn't exist. Skipping...")
continue
# list all .html files in chosen directory (there should only be one)
os.chdir(folderpath)
for file in glob.glob('*.html'):
filename = file
os.chdir('..')
filepath = os.path.join(folderpath, filename)
#if .old version of file already, abort
if os.path.exists(filepath+'.old'):
print('File already converted. Skipping...') #TODO: skip instead of abort
continue
print(filename, end=" ")
#start html cleaning with beautifulsoup
with open(filepath, encoding='utf-8') as fp:
soup = BeautifulSoup(fp, "html.parser")
print(".", end=" ")
#shameless self-promotion
creditMe = soup.new_tag('meta', content='Converted to HTML by Vinay Janardhanam')
soup.head.append(creditMe)
print(".", end=" ")
#remove style tag from header
for s in soup('style'):
s.extract()
#replace <h1> with <h3>
for h1 in soup('h1'):
h1.name = 'h3'
#replace <h2> with <h4>
for h2 in soup('h2'):
h2.name = 'h4'
#check for img tags with no alt attribute
for img in soup('img', alt=False):
img['alt'] = 'image'
print(".", end=" ")
#remove unnecessary attributes
for tag in soup():
for attribute in ['class', 'id', 'name', 'style']:
del tag[attribute]
print(".", end=" ")
#changes image path for KB site
#print(filename)
filename = os.path.splitext(filename)[0]
#print(filename)
for image in soup.findAll('img'):
image['src'] = image['src'].replace(filename, "/images/group87/"+KBID)
print(".", end=" ")
#preserves original html file with a .old extension
os.rename(filepath, filepath+'.old')
print(".", end=" ")
filetxt = os.path.join(folderpath, KBID)+'.txt'
#exports file with as HTML and TXT
with open(filepath, 'w', encoding="utf-8") as file:
file.write(str(soup.prettify()))
with open(filetxt, 'w', encoding="utf-8") as file:
file.write(str(soup.prettify()))
print(".", end=" ")
file_replace_text(filepath, '<p><br/></p>', '')
file_replace_text(filepath, '<body>', '<body>\n<div>')
file_replace_text(filepath, '</body>', '</div>\n</body>')
file_replace_text(filepath, '', '=>')
file_replace_text(filetxt, '<p><br/></p>', '')
file_replace_text(filetxt, '<body>', '<body>\n<div>')
file_replace_text(filetxt, '</body>', '</div>\n</body>')
file_replace_text(filetxt, '', '=>')
print('done!', end=" ")
sys.stdout.flush()
if __name__ == "__main__":
main()