-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliedbase.py
More file actions
253 lines (216 loc) · 8.64 KB
/
liedbase.py
File metadata and controls
253 lines (216 loc) · 8.64 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
from __future__ import print_function
from pptx import Presentation
import argparse
import common
import re
import helpers
import sys
sys.tracebacklimit = 0
# standaard bijbelvertaling
DEFAULT_BIBLE_TRANSLATION = "BGT"
# input file voor liturgie
LITURGIE_FILENAME = "liturgie.txt"
# pptx template naam
TEMPLATE_FILENAME = "template.pptx"
# resulterende output file
OUTPUT_FILENAME = 'presentatie.pptx'
# aantal regels bij liederen per sheet
MAX_NUM_LINES_PER_SHEET = 4
# naam van tekst placeholder in pptx template. waarschijnlijk niet veranderen.
TEMPLATE_TEXT_PLACEHOLDER = "Text Placeholder 1"
liedboeken = ['gezang', 'levenslied', 'lied', 'opwekking', 'psalm']
def parse_args():
""" Setup the input and output arguments for the script
Return the parsed input and output files
"""
parser = argparse.ArgumentParser(
description='Analyze powerpoint file structure and create chuch presentation with it')
parser.add_argument('analyze',
type=str,
nargs='?',
help='"analyze" voor analyze presentatie template')
return parser.parse_args()
def readInputFile(inputFile):
f = open(inputFile, 'r')
# loop over lines to filter empty lines
for line in f.readlines():
# remove unwanted spaces and newlines
line = line.strip()
# treat lines starting with hash-sign as comment
if not line or line[0:1] == '#':
continue
# validate syntax
if(helpers.validate_line(line.lower())):
getLiturgyLineContent(line)
def getLiturgyLineContent(line):
print("Slides maken voor: {}".format(line))
source = line[0:line.find(' ')]
if helpers.get_bijbelboek(line) != None:
bijbeltekst = get_bijbeltekst_from_source(line)
maak_slides(bijbeltekst)
elif source in liedboeken:
source_path = "bronnen/liederen/{}.txt".format(source)
print("Als bron wordt gebruikt: {}".format(source_path))
# haal liedtekst op
f = open(source_path, 'r')
# alle liederen met verzen
if(source != "opwekking"):
liederen = get_lines_from_source(line, source, f)
else:
# opwekking, zonder verzen
liederen = get_opwekking_from_source(line, source, f)
maak_slides(liederen)
else:
print("Dit type slide wordt (nog) niet ondersteund")
def get_lines_from_source(line, source, f):
# uitgangspunt: verzen worden altijd meegegeven als komma-gescheiden lijst.
# voorbeeld:
# psalm 4: 1
# gezang 14: 3, 6
songNumber = line[line.find(' '):line.find(':')].strip()
verses = line[line.find(':')+1:].split(',')
zoekstring = "^{} {}:".format(source, songNumber)
print("De volgende zoekstring wordt gebruikt: {}".format(zoekstring))
liedGevonden = False
versGevonden = False
currentVerse = -1
curr_num_lines = 0
# gevonden regels opslaan in 2d array
liederen = {}
# for verse in verses:
# liederen[verse] = []
for regel in f.readlines():
regel = regel.strip()
# zoek het juiste lied op
if(re.search(zoekstring, regel)):
liedGevonden = True
continue
# bij het volgende lied stoppen
if(re.search("^{} ".format(source), regel)):
if liedGevonden:
break
if liedGevonden:
if(regel in verses):
currentVerse = regel
versGevonden = True
liederen[currentVerse] = []
curr_num_lines = 0
continue
elif("" == regel or (common.is_integer(regel) and regel not in verses)):
versGevonden = False
if(versGevonden):
if curr_num_lines == MAX_NUM_LINES_PER_SHEET:
currentVerse = currentVerse + 'a'
curr_num_lines = 0
# add the new subverse to the dictionary
liederen[currentVerse] = []
liederen[currentVerse].append(regel)
curr_num_lines += 1
return liederen
def get_opwekking_from_source(line, source, f):
# uitgangspunt: opwekkingsliederen kennen geen verzen
# dus het hele lied wordt altijd gekopieerd.
# Lied wordt niet opgeknipt in 4 regels per sheet, omdat opwekking liederen
# onvoorspelbaarder zijn qua structuur.
songNumber = line[line.find(' '):].strip()
zoekstring = "^{} {}".format(source, songNumber)
print("De volgende zoekstring wordt gebruikt: {}".format(zoekstring))
liedGevonden = False
# curr_num_lines = 0
# gevonden regels opslaan in 2d array met virtueel en fixed vers nummer '1'
liederen = {}
currentVerse = 'a'
liederen[currentVerse] = [] # initialiseren
for regel in f.readlines():
regel = regel.strip()
# zoek het juiste lied op
if(re.search(zoekstring, regel)):
liedGevonden = True
continue
# bij het volgende lied stoppen
if(re.search("^{} ".format(source), regel)):
if liedGevonden:
break
if liedGevonden:
# if curr_num_lines == MAX_NUM_LINES_PER_SHEET:
# currentVerse = currentVerse + 'a'
# curr_num_lines = 0
# # add the new subverse to the dictionary
# liederen[currentVerse] = []
liederen[currentVerse].append(regel)
# curr_num_lines += 1
return liederen
def get_bijbeltekst_from_source(line):
source = helpers.get_source(line)
sourcepath = "./bronnen/bijbels/{}/{}.txt".format(
DEFAULT_BIBLE_TRANSLATION, source)
chapter = helpers.get_chapter(line)
van = helpers.get_van_vers(line)
tot = helpers.get_tot_vers(line)
print("Bijbeltekst slide maken met als bron: {}".format(sourcepath))
print("Boek {} hoofdstuk {} verzen {} tot en met {}".format(
source, chapter, van, tot))
lines = helpers.get_text_from_bible(
DEFAULT_BIBLE_TRANSLATION, source, chapter, van, tot)
if not lines:
raise Exception(
"\n\n####\nGeen bijbeltekst gevonden voor: {}. Controleer of het bijbelboek en de verzen bestaan en het niet om samengevoegde verzen gaat\n####\n".format(line))
inhoud = {}
inhoud['a'] = lines
return inhoud
def maak_slides(inhoud):
for liednummer, regels in inhoud.items():
textslide = prs.slides.add_slide(prs.slide_layouts[0])
for shape in textslide.placeholders:
if shape.is_placeholder:
phf = shape.placeholder_format
if shape.name == TEMPLATE_TEXT_PLACEHOLDER:
try:
shape.text = "\n".join(regels)
except AttributeError:
print("{} has no text attribute".format(phf.type))
def initialize_pptx(input):
""" Take the input file and analyze the structure.
The output file contains marked up information to make it easier
for generating future powerpoint templates.
"""
global prs
prs = Presentation(input)
def analyze_presentation(output):
""" Take the input file and analyze the structure.
The output file contains marked up information to make it easier
for generating future powerpoint templates.
"""
# Each powerpoint file has multiple layouts
# Loop through them all and see where the various elements are
for index, _ in enumerate(prs.slide_layouts):
slide = prs.slides.add_slide(prs.slide_layouts[index])
# Not every slide has to have a title
try:
title = slide.shapes.title
title.text = 'Title for Layout {}'.format(index)
except AttributeError:
print("No Title for Layout {}".format(index))
# Go through all the placeholders and identify them by index and type
for shape in slide.placeholders:
if shape.is_placeholder:
phf = shape.placeholder_format
# Do not overwrite the title which is just a special placeholder
try:
if 'Title' not in shape.text:
shape.text = '{}'.format(shape.name)
except AttributeError:
print("{} has no text attribute".format(phf.type))
print('{} {}'.format(phf.idx, shape.name))
prs.save(output)
def save_pptx(filename):
prs.save(filename)
if __name__ == "__main__":
args = parse_args()
initialize_pptx(TEMPLATE_FILENAME)
if args.analyze == 'analyze':
print("analyseer het template bestand")
analyze_presentation("analyze_{}".format(OUTPUT_FILENAME))
else:
readInputFile(LITURGIE_FILENAME)
save_pptx(OUTPUT_FILENAME)