-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWhatsappFormatter.py
82 lines (72 loc) · 2.36 KB
/
WhatsappFormatter.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
import os
import re
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Inches, Pt
path = "assets/"
regexp = re.compile(r'^(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.\d\d')
def formatFile(file):
newContent = ""
fullLine = ""
hasContent = False
for line in file:
if (regexp.match(line)):
if(hasContent):
hasContent = False
newContent += fullLine
fullLine = line
else:
fullLine = fullLine.replace("\n", " ")
fullLine += line
hasContent = True
file.seek(0)
file.truncate()
file.write(newContent)
def transformFile(file):
document = Document()
oldDate = ""
lineNumber = 1
for line in file:
date = line.split(",",1)[0]
date = date.split(".")
date[2] = "20" + date[2]
date = date[0] + "." + date[1] + "." + date[2]
if (date != oldDate):
p = document.add_paragraph()
run = p.add_run("\n" + date)
run.bold = True
font = run.font
font.size = Pt(12)
paragraph_format = p.paragraph_format
paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
oldDate = date
t = document.add_paragraph()
time = line.split(", ",1)[1]
timeNoSec = time.split(":", 2)
timeNoSec = timeNoSec[0] + ":" + timeNoSec[1]
t.add_run(timeNoSec).italic = True
if(line.count(':') > 3):
name = time.split(":")[3] + ":\t"
t.add_run(name).bold = True
text = time.split(":")[4].replace(" ", "", 1).replace("\n", " ")
t.add_run(text)
else:
text = time.split(":", 3)[3].replace(": ", ":\t", 1).replace("\n", " ")
t.add_run(text)
tFormat = t.paragraph_format
#print(text)
tFormat.left_indent = Inches(2.5)
tFormat.first_line_indent = Inches(-2.5)
print(lineNumber)
lineNumber += 1
documentName = (file.name).replace(".txt", ".docx")
document.save(documentName)
for filename in os.listdir(path):
if filename.endswith(".txt"):
filename = os.path.join(path, filename)
file = open(filename, 'r+')
formatFile(file)
file.close()
file = open(filename, 'r+')
transformFile(file)
file.close()