-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
64 lines (56 loc) · 1.73 KB
/
generator.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
#!/usr/bin/env python
# encoding: utf-8
import sys
from anytree.importer import IndentedStringImporter
from pptx import Presentation
from anytree import RenderTree
from anytree import LevelOrderIter
def ascii_tree(tree):
for pre, fill, node in RenderTree(tree):
print("%s%s" % (pre, node.name.decode("utf-8")))
def create_slide(ppt, nodes):
bullet_slide_layout = ppt.slide_layouts[1]
slide = ppt.slides.add_slide(bullet_slide_layout)
shapes = slide.shapes
title_shape = shapes.title
if nodes[0].parent.parent != None:
title_shape.text = "%s · %s" % (nodes[0].parent.name, nodes[0].name)
else:
title_shape.text = "%s" % (nodes[0].name)
body_shape = slide.shapes.placeholders
body_shape = slide.shapes.placeholders
for i in nodes[1:]:
new_paragraph = body_shape[1].text_frame.add_paragraph()
new_paragraph.text = i.name
new_paragraph.level = 1
def walk(tree):
slide = []
for i in LevelOrderIter(tree, maxlevel=2):
slide.append(i)
return slide
def run(ppt, tree):
if len(tree.children) == 0:
return
titles = walk(tree)
for i in titles[1:]:
t = walk(i)
create_slide(ppt, t)
run(ppt, i)
def main():
if len(sys.argv) != 3:
print("Usage: ")
print("\tpython %s [INPUT] [OUTPUT]" % (sys.argv[0]))
print("Example: ")
print("\tpython %s input.txt output.pptx" % (sys.argv[0]))
exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
ppt = Presentation()
data = open(input_file).read()
importer = IndentedStringImporter()
root = importer.import_(data)
ascii_tree(root)
run(ppt, root)
ppt.save(output_file)
if __name__ == '__main__':
main()