-
Notifications
You must be signed in to change notification settings - Fork 0
/
unprocessed_csv_extraction.py
55 lines (51 loc) · 1.99 KB
/
unprocessed_csv_extraction.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
import docx
import csv
import os
import codecs
with codecs.open('unprocessed_text.csv', 'a', 'utf-8') as f:
header = ['name', 'text']
# create the csv writer
writer = csv.writer(f)
writer.writerow(header)
f.close()
for filename in os.listdir("files"):
document = docx.Document("files/" + filename)
list_all = []
for paragraph in document.paragraphs:
for continues_text_with_same_fond in paragraph.runs:
if continues_text_with_same_fond.bold:
list_all.append([0, continues_text_with_same_fond.text])
# print("Bold: " + continues_text_with_same_fond.text)
else:
list_all.append([1, continues_text_with_same_fond.text])
# print("Text: " + continues_text_with_same_fond.text)
# open the file in the append mode
with codecs.open('unprocessed_text.csv', 'a', 'utf-8') as f:
# create the csv writer
writer = csv.writer(f)
# Find the first bold text
position = 0
for i in range(len(list_all)):
if list_all[i][0] == 0:
position = i
break
text = ""
name = ""
for i in range(position, len(list_all)):
# If text is bold then append the name list
if list_all[i][0] == 0:
name += list_all[i][1]
# Else the text is not bold, so it is the speech part
else:
text += list_all[i][1]
# If next text is bold
if i+1 < len(list_all)-1 and list_all[i + 1][0] == 0:
# If next text is not only spaces
if not list_all[i + 1][1].replace(" ", "") == "":
# write a row to the csv file
writer.writerow([name, text])
name = ""
text = ""
else:
text += " "
f.close()