barunsaha commited on
Commit
07115d9
1 Parent(s): 9383b85

Add hierarchical list of bullets from JSON to the slides

Browse files
Files changed (1) hide show
  1. pptx_helper.py +123 -21
pptx_helper.py CHANGED
@@ -1,4 +1,4 @@
1
- from typing import List
2
  import json5
3
  import pptx
4
  import re
@@ -81,24 +81,16 @@ def generate_powerpoint_presentation(
81
  all_headers.append(title_shape.text)
82
  text_frame = body_shape.text_frame
83
 
84
- for an_item in a_slide['bullet_points']:
85
- item_type = type(an_item)
86
- # print('Bullet point type:', item_type)
87
-
88
- if item_type is str:
89
- paragraph = text_frame.add_paragraph()
90
- paragraph.text = an_item
91
- paragraph.level = 0
92
- elif item_type is list:
93
- for sub_item in an_item:
94
- if type(sub_item) is str:
95
- paragraph = text_frame.add_paragraph()
96
- paragraph.text = sub_item
97
- paragraph.level = 1
98
-
99
- # background = slide.background
100
- # background.fill.gradient()
101
- # background.fill.gradient_angle = -225.0
102
 
103
  # The thank-you slide
104
  last_slide_layout = presentation.slide_layouts[0]
@@ -111,9 +103,119 @@ def generate_powerpoint_presentation(
111
  return all_headers
112
 
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  if __name__ == '__main__':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  generate_powerpoint_presentation(
116
- json5.loads(open('examples/example_02_structured_output.json', 'r').read()),
117
  as_yaml=False,
118
- output_file_name='test.pptx'
 
119
  )
 
1
+ from typing import List, Tuple
2
  import json5
3
  import pptx
4
  import re
 
81
  all_headers.append(title_shape.text)
82
  text_frame = body_shape.text_frame
83
 
84
+ # The bullet_points may contain a nested hierarchy of JSON arrays
85
+ # In some scenarios, it may contain objects (dictionaries) because the LLM generated so
86
+ # ^ The second scenario is not covered
87
+
88
+ flat_items_list = get_flat_list_of_contents(a_slide['bullet_points'], level=0)
89
+
90
+ for an_item in flat_items_list:
91
+ paragraph = text_frame.add_paragraph()
92
+ paragraph.text = an_item[0]
93
+ paragraph.level = an_item[1]
 
 
 
 
 
 
 
 
94
 
95
  # The thank-you slide
96
  last_slide_layout = presentation.slide_layouts[0]
 
103
  return all_headers
104
 
105
 
106
+ def get_flat_list_of_contents(items: list, level: int) -> List[Tuple]:
107
+ """
108
+ Flatten a (hierarchical) list of bullet points to a single list containing each item and its level.
109
+
110
+ :param items: A bullet point (string or list)
111
+ :param level: The current level of hierarchy
112
+ :return: A list of (bullet item text, hierarchical level) tuples
113
+ """
114
+
115
+ flat_list = []
116
+
117
+ for item in items:
118
+ if isinstance(item, str):
119
+ flat_list.append((item, level))
120
+ elif isinstance(item, list):
121
+ flat_list = flat_list + get_flat_list_of_contents(item, level + 1)
122
+
123
+ return flat_list
124
+
125
+
126
  if __name__ == '__main__':
127
+ # bullets = [
128
+ # 'Description',
129
+ # 'Types',
130
+ # [
131
+ # 'Type A',
132
+ # 'Type B'
133
+ # ],
134
+ # 'Grand parent',
135
+ # [
136
+ # 'Parent',
137
+ # [
138
+ # 'Grand child'
139
+ # ]
140
+ # ]
141
+ # ]
142
+
143
+ # output = get_flat_list_of_contents(bullets, level=0)
144
+ # for x in output:
145
+ # print(x)
146
+
147
+ json_data = '''
148
+ {
149
+ "title": "Understanding AI",
150
+ "slides": [
151
+ {
152
+ "heading": "Introduction",
153
+ "bullet_points": [
154
+ "Brief overview of AI",
155
+ [
156
+ "Importance of understanding AI"
157
+ ]
158
+ ]
159
+ },
160
+ {
161
+ "heading": "What is AI?",
162
+ "bullet_points": [
163
+ "Definition of AI",
164
+ [
165
+ "Types of AI",
166
+ [
167
+ "Narrow or weak AI",
168
+ "General or strong AI"
169
+ ]
170
+ ],
171
+ "Differences between AI and machine learning"
172
+ ]
173
+ },
174
+ {
175
+ "heading": "How AI Works",
176
+ "bullet_points": [
177
+ "Overview of AI algorithms",
178
+ [
179
+ "Types of AI algorithms",
180
+ [
181
+ "Rule-based systems",
182
+ "Decision tree systems",
183
+ "Neural networks"
184
+ ]
185
+ ],
186
+ "How AI processes data"
187
+ ]
188
+ },
189
+ {
190
+ "heading": "Pros of AI",
191
+ "bullet_points": [
192
+ "Increased efficiency and productivity",
193
+ "Improved accuracy and precision",
194
+ "Enhanced decision-making capabilities",
195
+ "Personalized experiences"
196
+ ]
197
+ },
198
+ {
199
+ "heading": "Cons of AI",
200
+ "bullet_points": [
201
+ "Job displacement and loss of employment",
202
+ "Bias and discrimination",
203
+ "Privacy and security concerns",
204
+ "Dependence on technology"
205
+ ]
206
+ },
207
+ {
208
+ "heading": "Future Prospects of AI",
209
+ "bullet_points": [
210
+ "Advancements in fields such as healthcare and finance",
211
+ "Increased use"
212
+ ]
213
+ }
214
+ ]
215
+ }'''
216
  generate_powerpoint_presentation(
217
+ json5.loads(json_data),
218
  as_yaml=False,
219
+ output_file_name='test.pptx',
220
+ slides_template='Blank'
221
  )