forked from sharanya02/Text-file-to-handwritten-pdf-file
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxttohandwriting.py
106 lines (88 loc) · 2.79 KB
/
txttohandwriting.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from PIL import Image
from fpdf import FPDF
from PIL import Image
import os
BG = Image.open("myfont/bg.png")
sizeOfSheet = BG.width
gap, _ = 0, 0
allowedChars = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM,.-?!() 1234567890'
def writee(char):
global gap, _
if char == '\n':
pass
else:
char.lower()
cases = Image.open("myfont/%s.png" % char)
BG.paste(cases, (gap, _))
size = cases.width
gap += size
del cases
def letterwrite(word):
global gap, _
if gap > sizeOfSheet - 95 * (len(word)):
gap = 0
_ += 200
for letter in word:
if letter in allowedChars:
if letter.islower():
pass
elif letter.isupper():
letter = letter.lower()
letter += 'upper'
elif letter == '.':
letter = "fullstop"
elif letter == '!':
letter = 'exclamation'
elif letter == '?':
letter = 'question'
elif letter == ',':
letter = 'comma'
elif letter == '(':
letter = 'braketop'
elif letter == ')':
letter = 'braketcl'
elif letter == '-':
letter = 'hiphen'
writee(letter)
def worddd(Input):
wordlist = Input.split(' ')
for i in wordlist:
letterwrite(i)
writee('space')
if __name__ == '__main__':
try:
with open('boom.txt', 'r') as file:
data = file.read().replace('\n', '')
with open('final_output.pdf', 'w') as file:
pass
l = len(data)
nn = len(data) // 600
chunks, chunk_size = len(data), len(data) // (nn + 1)
p = [data[i:i + chunk_size] for i in range(0, chunks, chunk_size)]
for i in range(0, len(p)):
worddd(p[i])
writee('\n')
BG.save('%doutt.png' % i)
BG1 = Image.open("myfont/bg.png")
BG = BG1
gap = 0
_ = 0
except ValueError as E:
print("{}\nTry again".format(E))
imagelist = []
for i in range(0, len(p)):
imagelist.append('%doutt.png' % i)
#Converting images to pdf
#Source:https://datatofish.com/images-to-pdf-python/
def pdf_creation(PNG_FILE, flag=False):
rgba = Image.open(PNG_FILE)
rgb = Image.new('RGB', rgba.size, (255, 255, 255)) # white background
rgb.paste(rgba, mask=rgba.split()[3]) # paste using alpha channel as mask
rgb.save('final_output.pdf',
append=flag) #Now save multiple images in same pdf file
#First create a pdf file if not created
pdf_creation(imagelist.pop(0))
#Now I am opening each images and converting them to pdf
#Appending them to pdfs
for PNG_FILE in imagelist:
pdf_creation(PNG_FILE, flag=True)