-
Notifications
You must be signed in to change notification settings - Fork 1
/
to_text.py
48 lines (34 loc) · 1.1 KB
/
to_text.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
from bs4 import BeautifulSoup
from xml.sax.saxutils import unescape
import sys
import re
OUTPUT_FILE = "output.txt"
if(len(sys.argv) == 2):
input_file = sys.argv[1]
elif len(sys.argv == 3):
input_file = sys.argv[1]
OUTPUT_FILE = sys.argv[2]
else:
print "\n\nto_text.py will convert your Evernote .enex export into a plain text file.\nThis file's usuage is as follows:\n\npython to_text.py <input_xml_filename> [OPTIONAL]<output_filename>\n\n"
sys.exit()
print "Reading input file..."
with open(input_file, 'r') as f:
read_file = f.read()
print "Converting file..."
soup = BeautifulSoup(read_file)
journal_entries = soup.find_all('note')
notes = []
print "Cleaning file text..."
for entry in journal_entries:
dirty = entry.content.text
clean = re.sub("<.*?>", "", dirty)
clean = clean.replace('\n', ' ').replace('\r', '')
notes.append(clean)
all_text = "\n\n".join(notes)
all_text = all_text
all_text = all_text.encode('utf8')
all_text = unescape(all_text, {"'": "'", """: '"', " ": " "})
print "Writing output..."
with open(OUTPUT_FILE, 'w') as f:
f.write(all_text)
print "Finished\n"