-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parsing - baseline
148 lines (113 loc) · 3.72 KB
/
Parsing - baseline
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from html.parser import HTMLParser
import time
import colorama
import docx
import os
from Function2 import *
from colorama import *
taglist =[]
count0 = 0
count1 = 0
count2 = 0
count3 = 0
switch = 0
switch2= 0
class MyHTMLParser(HTMLParser):
global document
global switch2
global count1
global count2
global count3
global paragraph
def handle_starttag(self, tag, attrs):
global switch
if len(attrs) >0:
print("START:", tag, attrs)
if len (attrs) == 0:
print("START:", tag)
self.opentags=tag
taglist.append(tag)
if tag == 'fnote':
switch2 = 1
if taglist[-1] == 'p':
switch = 0
def handle_endtag(self, tag):
print("END:", tag)
taglist.remove(tag)
switch2 = 0
def handle_data(self, data):
global count1
global count2
global count3
global paragraph
global document
global switch
global switch2
print(Fore.BLUE + "DATA(" + taglist[-1] + ")-" + data + colorama.Style.RESET_ALL)
activetag = taglist[-1]
if activetag == 'h1':
document.add_heading(data, level=3)
#paragraph = document.add_paragraph ('')
paragraph =document.add_paragraph()
print ('111111')
return
if activetag == 'p':
if switch == 1:
paragraph.add_run(data).italic = False
print ('SWITCH -1 or enabled so run')
switch = 0
return
if switch == 0:
print('SWITCH -0 or disabled so new paragraph')
paragraph = document.add_paragraph(data)
print('222222')
return
if activetag == 'i':
paragraph.add_run(data).italic =True
switch = 1
print('333333')
return
if activetag == 'item':
paragraph = document.add_paragraph(data)
if activetag == 'fnote':
paragraph.add_footnote(data)
switch = 1
if switch2 == 1: # or on
switch = 1
if activetag == 'quote':
paragraph = document.add_paragraph(data)
#1 and 0 is run and new paragraph respectively
parser = MyHTMLParser()
mydict = CSVimport('volfilenames.csv')
for filename, volno in mydict.items():
count0 +=1
#Key - (U)nwrap (D)ecompose
# (S)elect (U)nwrap
# (S)elect (D)ecompose
soup = SoupFILEimport(filename)
U1(soup, ['div', 'front'])
SU(soup,'date')
SD(soup, 'style')
SD(soup, 'figure')
U1(soup, ['div', 'front'])
U2(soup, 'div', 'type', 'headnote')
U2(soup, 'div', 'type', 'copyright')
U2(soup, 'span', 'style', 'font-variant:small-caps')
R2(soup, 'span', 'class', 'head', 'h1')
R2(soup, 'span', 'class', 'item', 'item')
R2(soup, 'span', 'class', 'fnote', 'fnote')
R2(soup, 'span', 'style', 'font-style:italic', 'i')
R2(soup, 'span', 'style', 'font-style: italic;', 'i')
R2(soup, 'span', 'style', 'text-align:left', 'p')
R2(soup, 'span', 'style', 'text-align:right', 'p')
R3(soup, 'xmlns')
document = docx.Document()
paragraph = document.add_paragraph
parser.feed(str(soup))
os.system("pkill 'Microsoft Word'")
time.sleep(1)
document.save('./docx/temp/' + str(count0) + filename + '.docx')
#os.system("open /Applications/Microsoft\ Word.app " + '/Users/williamcorney/PycharmProjects/pythonProject/DOCX/temp/2' + filename + '.docx')
if count0 > 30:
quit()
# html = "<h1> heading</h1><p> paragraph <i> italics <b> bold </b> </i> should be paragraph </p><h2> heading </h2> <p> paragraph <i> italics </i> and more paragraph </p>"