mcpcn-office-powerpoint-mcp-server 2.1.0__py3-none-any.whl → 2.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
utils/template_utils.py CHANGED
@@ -1,1143 +1,1143 @@
1
- """
2
- Enhanced template management utilities for PowerPoint MCP Server.
3
- Advanced slide creation with dynamic sizing, auto-wrapping, and visual effects.
4
- Combines features from both basic and enhanced template systems.
5
- """
6
- import json
7
- import os
8
- import re
9
- from typing import Dict, List, Optional, Any, Tuple
10
- from pptx import Presentation
11
- from pptx.util import Inches, Pt
12
- from pptx.dml.color import RGBColor
13
- from pptx.enum.text import PP_ALIGN, MSO_VERTICAL_ANCHOR
14
- from pptx.enum.shapes import MSO_SHAPE
15
- import utils.content_utils as content_utils
16
- import utils.design_utils as design_utils
17
-
18
-
19
- class TextSizeCalculator:
20
- """Calculate optimal text sizes based on content and container dimensions."""
21
-
22
- def __init__(self):
23
- self.character_widths = {
24
- 'narrow': 0.6, # i, l, t
25
- 'normal': 1.0, # most characters
26
- 'wide': 1.3, # m, w
27
- 'space': 0.5 # space character
28
- }
29
-
30
- def estimate_text_width(self, text: str, font_size: int) -> float:
31
- """Estimate text width in points based on character analysis."""
32
- if not text:
33
- return 0
34
-
35
- width = 0
36
- for char in text:
37
- if char in 'iltj':
38
- width += self.character_widths['narrow']
39
- elif char in 'mwMW':
40
- width += self.character_widths['wide']
41
- elif char == ' ':
42
- width += self.character_widths['space']
43
- else:
44
- width += self.character_widths['normal']
45
-
46
- return width * font_size * 0.6 # Approximation factor
47
-
48
- def estimate_text_height(self, text: str, font_size: int, line_spacing: float = 1.2) -> float:
49
- """Estimate text height based on line count and spacing."""
50
- lines = len(text.split('\n'))
51
- return lines * font_size * line_spacing * 1.3 # Convert to points
52
-
53
- def calculate_optimal_font_size(self, text: str, container_width: float,
54
- container_height: float, font_type: str = 'body',
55
- min_size: int = 8, max_size: int = 36) -> int:
56
- """Calculate optimal font size to fit text in container."""
57
- container_width_pts = container_width * 72 # Convert inches to points
58
- container_height_pts = container_height * 72
59
-
60
- # Start with a reasonable size and adjust
61
- for font_size in range(max_size, min_size - 1, -1):
62
- estimated_width = self.estimate_text_width(text, font_size)
63
- estimated_height = self.estimate_text_height(text, font_size)
64
-
65
- if estimated_width <= container_width_pts * 0.9 and estimated_height <= container_height_pts * 0.9:
66
- return font_size
67
-
68
- return min_size
69
-
70
- def wrap_text_intelligently(self, text: str, max_width: float, font_size: int) -> str:
71
- """Intelligently wrap text to fit within specified width."""
72
- if not text:
73
- return text
74
-
75
- max_width_pts = max_width * 72
76
- words = text.split()
77
- wrapped_lines = []
78
- current_line = []
79
-
80
- for word in words:
81
- test_line = current_line + [word]
82
- test_text = ' '.join(test_line)
83
-
84
- if self.estimate_text_width(test_text, font_size) <= max_width_pts:
85
- current_line.append(word)
86
- else:
87
- if current_line:
88
- wrapped_lines.append(' '.join(current_line))
89
- current_line = [word]
90
- else:
91
- # Single word is too long, force wrap
92
- wrapped_lines.append(word)
93
-
94
- if current_line:
95
- wrapped_lines.append(' '.join(current_line))
96
-
97
- return '\n'.join(wrapped_lines)
98
-
99
-
100
- class VisualEffectsManager:
101
- """Manage and apply visual effects to PowerPoint elements."""
102
-
103
- def __init__(self, templates_data: Dict):
104
- self.templates_data = templates_data
105
- self.text_effects = templates_data.get('text_effects', {})
106
- self.image_effects = templates_data.get('image_effects', {})
107
-
108
- def apply_text_effects(self, text_frame, effects: List[str], color_scheme: str) -> None:
109
- """Apply text effects like shadows, glows, and outlines."""
110
- for effect_name in effects:
111
- if effect_name not in self.text_effects:
112
- continue
113
-
114
- effect_config = self.text_effects[effect_name]
115
- effect_type = effect_config.get('type')
116
-
117
- # Note: These are simplified implementations
118
- # Full implementation would require XML manipulation
119
- try:
120
- if effect_type == 'shadow':
121
- self._apply_text_shadow(text_frame, effect_config, color_scheme)
122
- elif effect_type == 'glow':
123
- self._apply_text_glow(text_frame, effect_config, color_scheme)
124
- elif effect_type == 'outline':
125
- self._apply_text_outline(text_frame, effect_config, color_scheme)
126
- except Exception:
127
- # Graceful fallback if effect application fails
128
- pass
129
-
130
- def _apply_text_shadow(self, text_frame, config: Dict, color_scheme: str) -> None:
131
- """Apply shadow effect to text (simplified implementation)."""
132
- # In a full implementation, this would manipulate the XML directly
133
- # For now, we'll apply basic formatting that creates a shadow-like effect
134
- for paragraph in text_frame.paragraphs:
135
- for run in paragraph.runs:
136
- # Make text slightly bolder to simulate shadow depth
137
- run.font.bold = True
138
-
139
- def _apply_text_glow(self, text_frame, config: Dict, color_scheme: str) -> None:
140
- """Apply glow effect to text (simplified implementation)."""
141
- pass # Would require XML manipulation for true glow effect
142
-
143
- def _apply_text_outline(self, text_frame, config: Dict, color_scheme: str) -> None:
144
- """Apply outline effect to text (simplified implementation)."""
145
- pass # Would require XML manipulation for true outline effect
146
-
147
- def apply_image_effects(self, image_shape, effect_name: str, color_scheme: str) -> None:
148
- """Apply visual effects to image shapes."""
149
- if effect_name not in self.image_effects:
150
- return
151
-
152
- effect_config = self.image_effects[effect_name]
153
-
154
- try:
155
- # Apply shadow if specified
156
- if 'shadow' in effect_config:
157
- shadow_config = effect_config['shadow']
158
- # Simplified shadow application
159
- pass
160
-
161
- # Apply border if specified
162
- if 'border' in effect_config:
163
- border_config = effect_config['border']
164
- if 'width' in border_config:
165
- image_shape.line.width = Pt(border_config['width'])
166
- if 'color_role' in border_config:
167
- color = self._get_color_from_scheme(color_scheme, border_config['color_role'])
168
- image_shape.line.color.rgb = RGBColor(*color)
169
- elif 'color' in border_config:
170
- image_shape.line.color.rgb = RGBColor(*border_config['color'])
171
-
172
- except Exception:
173
- # Graceful fallback
174
- pass
175
-
176
- def _get_color_from_scheme(self, color_scheme: str, color_role: str) -> Tuple[int, int, int]:
177
- """Get color from scheme (helper method)."""
178
- schemes = self.templates_data.get('color_schemes', {})
179
- if color_scheme in schemes and color_role in schemes[color_scheme]:
180
- return tuple(schemes[color_scheme][color_role])
181
- return (0, 0, 0) # Default black
182
-
183
-
184
- class EnhancedTemplateManager:
185
- """Enhanced template manager with dynamic features."""
186
-
187
- def __init__(self, template_file_path: str = None):
188
- self.text_calculator = TextSizeCalculator()
189
- self.load_templates(template_file_path)
190
- self.effects_manager = VisualEffectsManager(self.templates_data)
191
-
192
- def load_templates(self, template_file_path: str = None) -> None:
193
- """Load unified templates with all dynamic features."""
194
- if template_file_path is None:
195
- current_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
196
- # Use the unified template file
197
- template_file_path = os.path.join(current_dir, 'slide_layout_templates.json')
198
-
199
- try:
200
- with open(template_file_path, 'r', encoding='utf-8') as f:
201
- self.templates_data = json.load(f)
202
- except FileNotFoundError:
203
- raise FileNotFoundError(f"Template file not found: {template_file_path}")
204
- except json.JSONDecodeError as e:
205
- raise ValueError(f"Invalid JSON in template file: {str(e)}")
206
-
207
-
208
- def get_dynamic_font_size(self, element: Dict, content: str = None) -> int:
209
- """Calculate dynamic font size based on content and container."""
210
- content = content or element.get('placeholder_text', '')
211
- if not content:
212
- return 14 # Default size
213
-
214
- # Get container dimensions
215
- pos = element.get('position', {})
216
- container_width = pos.get('width', 4.0)
217
- container_height = pos.get('height', 1.0)
218
-
219
- # Get font constraints
220
- font_type = element.get('styling', {}).get('font_type', 'body')
221
- sizing_rules = self.templates_data.get('auto_sizing_rules', {})
222
- base_sizes = sizing_rules.get('text_measurement', {}).get('base_font_sizes', {})
223
-
224
- if font_type in base_sizes:
225
- min_size = base_sizes[font_type]['min']
226
- max_size = base_sizes[font_type]['max']
227
- default_size = base_sizes[font_type]['default']
228
- else:
229
- min_size, max_size, default_size = 10, 18, 14
230
-
231
- # Check if dynamic sizing is requested
232
- font_size_setting = element.get('styling', {}).get('font_size')
233
- if font_size_setting == 'dynamic':
234
- return self.text_calculator.calculate_optimal_font_size(
235
- content, container_width, container_height, font_type, min_size, max_size
236
- )
237
-
238
- return default_size
239
-
240
- def apply_enhanced_slide_template(self, slide, template_id: str, color_scheme: str = 'modern_blue',
241
- content_mapping: Dict = None, image_paths: Dict = None) -> Dict:
242
- """Apply enhanced slide template with all dynamic features."""
243
- try:
244
- if template_id not in self.templates_data.get('templates', {}):
245
- # Fall back to regular template application
246
- return apply_slide_template_basic(slide, template_id, color_scheme, content_mapping, image_paths)
247
-
248
- template = self.templates_data['templates'][template_id]
249
- elements_created = []
250
-
251
- # Apply enhanced background if specified
252
- background_config = template.get('background')
253
- if background_config:
254
- apply_slide_background(slide, background_config, self.templates_data, color_scheme)
255
-
256
- # Create enhanced elements
257
- for element in template.get('elements', []):
258
- element_type = element.get('type')
259
- element_role = element.get('role', '')
260
-
261
- try:
262
- # Override content if provided
263
- custom_content = None
264
- if content_mapping and element_role in content_mapping:
265
- custom_content = content_mapping[element_role]
266
-
267
- created_element = None
268
-
269
- if element_type == 'text':
270
- created_element = self.create_enhanced_text_element(
271
- slide, element, self.templates_data, color_scheme, custom_content
272
- )
273
- elif element_type == 'shape':
274
- created_element = create_shape_element(slide, element, self.templates_data, color_scheme)
275
- elif element_type == 'image':
276
- image_path = image_paths.get(element_role) if image_paths else None
277
- created_element = create_image_element(slide, element, image_path)
278
- elif element_type == 'table':
279
- created_element = create_table_element(slide, element, self.templates_data, color_scheme)
280
- elif element_type == 'chart':
281
- created_element = create_chart_element(slide, element, self.templates_data, color_scheme)
282
-
283
- if created_element:
284
- elements_created.append({
285
- 'type': element_type,
286
- 'role': element_role,
287
- 'index': len(slide.shapes) - 1,
288
- 'enhanced_features': self.get_element_features(element)
289
- })
290
-
291
- except Exception as e:
292
- elements_created.append({
293
- 'type': element_type,
294
- 'role': element_role,
295
- 'error': str(e)
296
- })
297
-
298
- return {
299
- 'success': True,
300
- 'template_id': template_id,
301
- 'template_name': template.get('name', template_id),
302
- 'color_scheme': color_scheme,
303
- 'elements_created': elements_created,
304
- 'enhanced_features_applied': [
305
- 'Dynamic text sizing',
306
- 'Automatic text wrapping',
307
- 'Visual effects',
308
- 'Intelligent content adaptation'
309
- ]
310
- }
311
-
312
- except Exception as e:
313
- return {
314
- 'success': False,
315
- 'error': f"Failed to apply enhanced template: {str(e)}"
316
- }
317
-
318
- def create_enhanced_text_element(self, slide, element: Dict, templates_data: Dict,
319
- color_scheme: str, custom_content: str = None) -> Any:
320
- """Create text element with enhanced features."""
321
- pos = element['position']
322
-
323
- # Determine content
324
- content = custom_content or element.get('placeholder_text', '')
325
-
326
- # Apply auto-wrapping if enabled
327
- styling = element.get('styling', {})
328
- if styling.get('auto_wrap', False):
329
- container_width = pos.get('width', 4.0)
330
- font_size = self.get_dynamic_font_size(element, content)
331
- content = self.text_calculator.wrap_text_intelligently(content, container_width, font_size)
332
-
333
- # Create text box
334
- textbox = slide.shapes.add_textbox(
335
- Inches(pos['left']),
336
- Inches(pos['top']),
337
- Inches(pos['width']),
338
- Inches(pos['height'])
339
- )
340
-
341
- textbox.text_frame.text = content
342
- textbox.text_frame.word_wrap = True
343
-
344
- # Apply dynamic font sizing
345
- font_size = self.get_dynamic_font_size(element, content)
346
-
347
- # Apply enhanced styling
348
- self.apply_enhanced_text_styling(textbox.text_frame, element, templates_data, color_scheme, font_size)
349
-
350
- # Apply auto-fit if enabled
351
- if styling.get('auto_fit', False):
352
- textbox.text_frame.auto_size = True
353
-
354
- return textbox
355
-
356
- def apply_enhanced_text_styling(self, text_frame, element: Dict, templates_data: Dict,
357
- color_scheme: str, font_size: int) -> None:
358
- """Apply enhanced text styling with effects and dynamic features."""
359
- styling = element.get('styling', {})
360
-
361
- # Get typography style
362
- typography_style = templates_data.get('typography_styles', {}).get('modern_sans', {})
363
- font_type = styling.get('font_type', 'body')
364
- font_config = typography_style.get(font_type, {'name': 'Segoe UI', 'weight': 'normal'})
365
-
366
- # Color handling
367
- color = None
368
- if 'color_role' in styling:
369
- color = get_color_from_scheme(templates_data, color_scheme, styling['color_role'])
370
- elif 'color' in styling:
371
- color = tuple(styling['color'])
372
-
373
- # Alignment mapping
374
- alignment_map = {
375
- 'left': PP_ALIGN.LEFT,
376
- 'center': PP_ALIGN.CENTER,
377
- 'right': PP_ALIGN.RIGHT,
378
- 'justify': PP_ALIGN.JUSTIFY
379
- }
380
-
381
- # Vertical alignment mapping
382
- vertical_alignment_map = {
383
- 'top': MSO_VERTICAL_ANCHOR.TOP,
384
- 'middle': MSO_VERTICAL_ANCHOR.MIDDLE,
385
- 'bottom': MSO_VERTICAL_ANCHOR.BOTTOM
386
- }
387
-
388
- # Apply vertical alignment to text frame
389
- if 'vertical_alignment' in styling:
390
- v_align = styling['vertical_alignment']
391
- if v_align in vertical_alignment_map:
392
- text_frame.vertical_anchor = vertical_alignment_map[v_align]
393
-
394
- # Dynamic line spacing
395
- line_spacing = styling.get('line_spacing', 1.2)
396
- if line_spacing == 'dynamic':
397
- content_length = len(text_frame.text)
398
- if content_length > 300:
399
- line_spacing = 1.4
400
- elif content_length > 150:
401
- line_spacing = 1.3
402
- else:
403
- line_spacing = 1.2
404
-
405
- # Apply formatting to paragraphs and runs
406
- for paragraph in text_frame.paragraphs:
407
- # Set alignment
408
- if 'alignment' in styling and styling['alignment'] in alignment_map:
409
- paragraph.alignment = alignment_map[styling['alignment']]
410
-
411
- # Set line spacing
412
- paragraph.line_spacing = line_spacing
413
-
414
- # Apply formatting to runs
415
- for run in paragraph.runs:
416
- font = run.font
417
-
418
- # Font family and size
419
- font.name = font_config['name']
420
- font.size = Pt(font_size)
421
-
422
- # Font weight and style
423
- weight = font_config.get('weight', 'normal')
424
- font.bold = styling.get('bold', weight in ['bold', 'semibold'])
425
- font.italic = styling.get('italic', font_config.get('style') == 'italic')
426
- font.underline = styling.get('underline', False)
427
-
428
- # Color
429
- if color:
430
- font.color.rgb = RGBColor(*color)
431
-
432
- # Apply text effects
433
- text_effects = styling.get('text_effects', [])
434
- if text_effects:
435
- self.effects_manager.apply_text_effects(text_frame, text_effects, color_scheme)
436
-
437
- def get_element_features(self, element: Dict) -> List[str]:
438
- """Get list of enhanced features applied to an element."""
439
- features = []
440
- styling = element.get('styling', {})
441
-
442
- if styling.get('font_size') == 'dynamic':
443
- features.append('Dynamic text sizing')
444
- if styling.get('auto_wrap'):
445
- features.append('Automatic text wrapping')
446
- if styling.get('text_effects'):
447
- features.append('Text visual effects')
448
- if styling.get('auto_fit'):
449
- features.append('Auto-fit content')
450
- if 'fill_gradient' in styling:
451
- features.append('Gradient fills')
452
- if styling.get('shadow') or styling.get('glow'):
453
- features.append('Advanced visual effects')
454
-
455
- return features
456
-
457
-
458
- # Global instance for enhanced features
459
- enhanced_template_manager = EnhancedTemplateManager()
460
-
461
-
462
- def get_enhanced_template_manager() -> EnhancedTemplateManager:
463
- """Get the global enhanced template manager instance."""
464
- return enhanced_template_manager
465
-
466
-
467
- def calculate_dynamic_font_size(text: str, container_width: float, container_height: float,
468
- font_type: str = 'body') -> int:
469
- """Calculate optimal font size for given text and container."""
470
- return enhanced_template_manager.text_calculator.calculate_optimal_font_size(
471
- text, container_width, container_height, font_type
472
- )
473
-
474
-
475
- def wrap_text_automatically(text: str, container_width: float, font_size: int) -> str:
476
- """Automatically wrap text to fit container width."""
477
- return enhanced_template_manager.text_calculator.wrap_text_intelligently(
478
- text, container_width, font_size
479
- )
480
-
481
-
482
- def load_slide_templates(template_file_path: str = None) -> Dict:
483
- """
484
- Load slide layout templates from JSON file.
485
-
486
- Args:
487
- template_file_path: Path to template JSON file (defaults to slide_layout_templates.json)
488
-
489
- Returns:
490
- Dictionary containing all template definitions
491
- """
492
- if template_file_path is None:
493
- # Default to the template file in the same directory as the script
494
- current_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
495
- template_file_path = os.path.join(current_dir, 'slide_layout_templates.json')
496
-
497
- try:
498
- with open(template_file_path, 'r', encoding='utf-8') as f:
499
- templates = json.load(f)
500
- return templates
501
- except FileNotFoundError:
502
- raise FileNotFoundError(f"Template file not found: {template_file_path}")
503
- except json.JSONDecodeError as e:
504
- raise ValueError(f"Invalid JSON in template file: {str(e)}")
505
-
506
-
507
- def get_available_templates() -> List[Dict]:
508
- """
509
- Get a list of all available slide templates.
510
-
511
- Returns:
512
- List of template information dictionaries
513
- """
514
- try:
515
- templates_data = load_slide_templates()
516
- template_list = []
517
-
518
- for template_id, template_info in templates_data.get('templates', {}).items():
519
- template_list.append({
520
- 'id': template_id,
521
- 'name': template_info.get('name', template_id),
522
- 'description': template_info.get('description', ''),
523
- 'layout_type': template_info.get('layout_type', 'content'),
524
- 'element_count': len(template_info.get('elements', []))
525
- })
526
-
527
- return template_list
528
- except Exception as e:
529
- return [{'error': f"Failed to load templates: {str(e)}"}]
530
-
531
-
532
- def get_color_from_scheme(templates_data: Dict, color_scheme: str, color_role: str) -> Tuple[int, int, int]:
533
- """
534
- Get RGB color values from a color scheme.
535
-
536
- Args:
537
- templates_data: Template data dictionary
538
- color_scheme: Name of the color scheme
539
- color_role: Role of the color (primary, secondary, accent1, etc.)
540
-
541
- Returns:
542
- RGB color tuple (r, g, b)
543
- """
544
- color_schemes = templates_data.get('color_schemes', {})
545
-
546
- if color_scheme not in color_schemes:
547
- color_scheme = 'modern_blue' # Default fallback
548
-
549
- scheme = color_schemes[color_scheme]
550
- return tuple(scheme.get(color_role, scheme.get('primary', [0, 120, 215])))
551
-
552
-
553
- def get_font_settings(templates_data: Dict, font_type: str, font_size: str) -> Dict:
554
- """
555
- Get font settings from typography configuration.
556
-
557
- Args:
558
- templates_data: Template data dictionary
559
- font_type: Type of font (title, subtitle, body, caption)
560
- font_size: Size category (large, medium, small)
561
-
562
- Returns:
563
- Dictionary with font settings
564
- """
565
- typography = templates_data.get('typography', {})
566
-
567
- if font_type not in typography:
568
- font_type = 'body' # Default fallback
569
-
570
- font_config = typography[font_type]
571
- size_key = f'font_size_{font_size}'
572
-
573
- return {
574
- 'name': font_config.get('font_name', 'Segoe UI'),
575
- 'size': font_config.get(size_key, font_config.get('font_size_medium', 14)),
576
- 'bold': font_config.get('bold', False)
577
- }
578
-
579
-
580
- def apply_text_styling(text_frame, styling: Dict, templates_data: Dict, color_scheme: str) -> None:
581
- """
582
- Apply text styling based on template configuration.
583
-
584
- Args:
585
- text_frame: PowerPoint text frame object
586
- styling: Styling configuration from template
587
- templates_data: Template data dictionary
588
- color_scheme: Selected color scheme
589
- """
590
- # Get font settings
591
- font_type = styling.get('font_type', 'body')
592
- font_size_category = styling.get('font_size', 'medium')
593
- font_settings = get_font_settings(templates_data, font_type, font_size_category)
594
-
595
- # Get color
596
- color = None
597
- if 'color_role' in styling:
598
- color = get_color_from_scheme(templates_data, color_scheme, styling['color_role'])
599
- elif 'color' in styling:
600
- color = tuple(styling['color'])
601
-
602
- # Apply alignment
603
- alignment_map = {
604
- 'left': PP_ALIGN.LEFT,
605
- 'center': PP_ALIGN.CENTER,
606
- 'right': PP_ALIGN.RIGHT,
607
- 'justify': PP_ALIGN.JUSTIFY
608
- }
609
-
610
- # Apply formatting to all paragraphs and runs
611
- for paragraph in text_frame.paragraphs:
612
- if 'alignment' in styling and styling['alignment'] in alignment_map:
613
- paragraph.alignment = alignment_map[styling['alignment']]
614
-
615
- for run in paragraph.runs:
616
- font = run.font
617
- font.name = font_settings['name']
618
- font.size = Pt(font_settings['size'])
619
- font.bold = styling.get('bold', font_settings['bold'])
620
- font.italic = styling.get('italic', False)
621
- font.underline = styling.get('underline', False)
622
-
623
- if color:
624
- font.color.rgb = RGBColor(*color)
625
-
626
-
627
- def create_text_element(slide, element: Dict, templates_data: Dict, color_scheme: str) -> Any:
628
- """
629
- Create a text element on a slide based on template configuration.
630
-
631
- Args:
632
- slide: PowerPoint slide object
633
- element: Element configuration from template
634
- templates_data: Template data dictionary
635
- color_scheme: Selected color scheme
636
-
637
- Returns:
638
- Created text box shape
639
- """
640
- pos = element['position']
641
- textbox = slide.shapes.add_textbox(
642
- Inches(pos['left']),
643
- Inches(pos['top']),
644
- Inches(pos['width']),
645
- Inches(pos['height'])
646
- )
647
-
648
- # Set text content
649
- textbox.text_frame.text = element.get('placeholder_text', '')
650
-
651
- # Apply styling
652
- styling = element.get('styling', {})
653
- apply_text_styling(textbox.text_frame, styling, templates_data, color_scheme)
654
-
655
- return textbox
656
-
657
-
658
- def create_image_element(slide, element: Dict, image_path: str = None) -> Any:
659
- """
660
- Create an image element on a slide based on template configuration.
661
-
662
- Args:
663
- slide: PowerPoint slide object
664
- element: Element configuration from template
665
- image_path: Optional path to image file
666
-
667
- Returns:
668
- Created image shape or None if no image provided
669
- """
670
- if not image_path:
671
- # Create placeholder rectangle if no image provided
672
- pos = element['position']
673
- placeholder = slide.shapes.add_shape(
674
- 1, # Rectangle shape
675
- Inches(pos['left']),
676
- Inches(pos['top']),
677
- Inches(pos['width']),
678
- Inches(pos['height'])
679
- )
680
-
681
- # Add placeholder text
682
- if hasattr(placeholder, 'text_frame'):
683
- placeholder.text_frame.text = element.get('placeholder_text', 'Image Placeholder')
684
-
685
- return placeholder
686
-
687
- try:
688
- pos = element['position']
689
- image_shape = content_utils.add_image(
690
- slide,
691
- image_path,
692
- pos['left'],
693
- pos['top'],
694
- pos['width'],
695
- pos['height']
696
- )
697
-
698
- # Apply styling if specified
699
- styling = element.get('styling', {})
700
- if styling.get('shadow'):
701
- # Apply shadow effect (simplified)
702
- pass
703
-
704
- return image_shape
705
- except Exception:
706
- # Fallback to placeholder if image fails to load
707
- return create_image_element(slide, element, None)
708
-
709
-
710
- def create_shape_element(slide, element: Dict, templates_data: Dict, color_scheme: str) -> Any:
711
- """
712
- Create a shape element on a slide based on template configuration.
713
-
714
- Args:
715
- slide: PowerPoint slide object
716
- element: Element configuration from template
717
- templates_data: Template data dictionary
718
- color_scheme: Selected color scheme
719
-
720
- Returns:
721
- Created shape
722
- """
723
- pos = element['position']
724
- shape_type = element.get('shape_type', 'rectangle')
725
-
726
- try:
727
- # Import the shape creation function from the main server
728
- from ppt_mcp_server import add_shape_direct
729
- shape = add_shape_direct(slide, shape_type, pos['left'], pos['top'], pos['width'], pos['height'])
730
-
731
- # Apply styling
732
- styling = element.get('styling', {})
733
-
734
- # Fill color
735
- if 'fill_color_role' in styling:
736
- fill_color = get_color_from_scheme(templates_data, color_scheme, styling['fill_color_role'])
737
- shape.fill.solid()
738
- shape.fill.fore_color.rgb = RGBColor(*fill_color)
739
- elif 'fill_color' in styling:
740
- shape.fill.solid()
741
- shape.fill.fore_color.rgb = RGBColor(*styling['fill_color'])
742
-
743
- # Line color
744
- if 'line_color_role' in styling:
745
- line_color = get_color_from_scheme(templates_data, color_scheme, styling['line_color_role'])
746
- shape.line.color.rgb = RGBColor(*line_color)
747
- elif styling.get('no_border'):
748
- shape.line.fill.background()
749
-
750
- # Transparency
751
- if 'transparency' in styling:
752
- # Note: Transparency implementation would need additional XML manipulation
753
- pass
754
-
755
- return shape
756
- except Exception as e:
757
- # Create a simple rectangle as fallback
758
- textbox = slide.shapes.add_textbox(
759
- Inches(pos['left']),
760
- Inches(pos['top']),
761
- Inches(pos['width']),
762
- Inches(pos['height'])
763
- )
764
- textbox.text_frame.text = f"Shape: {shape_type}"
765
- return textbox
766
-
767
-
768
- def create_table_element(slide, element: Dict, templates_data: Dict, color_scheme: str) -> Any:
769
- """
770
- Create a table element on a slide based on template configuration.
771
-
772
- Args:
773
- slide: PowerPoint slide object
774
- element: Element configuration from template
775
- templates_data: Template data dictionary
776
- color_scheme: Selected color scheme
777
-
778
- Returns:
779
- Created table shape
780
- """
781
- pos = element['position']
782
- table_config = element.get('table_config', {})
783
-
784
- rows = table_config.get('rows', 3)
785
- cols = table_config.get('cols', 3)
786
-
787
- # Create table
788
- table_shape = content_utils.add_table(
789
- slide, rows, cols, pos['left'], pos['top'], pos['width'], pos['height']
790
- )
791
- table = table_shape.table
792
-
793
- # Populate with data if provided
794
- data = table_config.get('data', [])
795
- for r in range(min(rows, len(data))):
796
- for c in range(min(cols, len(data[r]))):
797
- table.cell(r, c).text = str(data[r][c])
798
-
799
- # Apply styling
800
- styling = element.get('styling', {})
801
- header_row = table_config.get('header_row', True)
802
-
803
- for r in range(rows):
804
- for c in range(cols):
805
- cell = table.cell(r, c)
806
-
807
- if r == 0 and header_row:
808
- # Header styling
809
- if 'header_bg_color_role' in styling:
810
- bg_color = get_color_from_scheme(templates_data, color_scheme, styling['header_bg_color_role'])
811
- cell.fill.solid()
812
- cell.fill.fore_color.rgb = RGBColor(*bg_color)
813
-
814
- # Header text color
815
- if 'header_text_color' in styling:
816
- for paragraph in cell.text_frame.paragraphs:
817
- for run in paragraph.runs:
818
- run.font.color.rgb = RGBColor(*styling['header_text_color'])
819
- run.font.bold = True
820
- else:
821
- # Body styling
822
- if 'body_bg_color_role' in styling:
823
- bg_color = get_color_from_scheme(templates_data, color_scheme, styling['body_bg_color_role'])
824
- cell.fill.solid()
825
- cell.fill.fore_color.rgb = RGBColor(*bg_color)
826
-
827
- return table_shape
828
-
829
-
830
- def create_chart_element(slide, element: Dict, templates_data: Dict, color_scheme: str) -> Any:
831
- """
832
- Create a chart element on a slide based on template configuration.
833
-
834
- Args:
835
- slide: PowerPoint slide object
836
- element: Element configuration from template
837
- templates_data: Template data dictionary
838
- color_scheme: Selected color scheme
839
-
840
- Returns:
841
- Created chart object
842
- """
843
- pos = element['position']
844
- chart_config = element.get('chart_config', {})
845
-
846
- chart_type = chart_config.get('type', 'column')
847
- categories = chart_config.get('categories', ['A', 'B', 'C'])
848
- series_data = chart_config.get('series', [{'name': 'Series 1', 'values': [1, 2, 3]}])
849
-
850
- # Extract series names and values
851
- series_names = [s['name'] for s in series_data]
852
- series_values = [s['values'] for s in series_data]
853
-
854
- try:
855
- # Create chart
856
- chart = content_utils.add_chart(
857
- slide, chart_type, pos['left'], pos['top'], pos['width'], pos['height'],
858
- categories, series_names, series_values
859
- )
860
-
861
- # Apply formatting
862
- chart_title = chart_config.get('title')
863
- if chart_title:
864
- content_utils.format_chart(chart, title=chart_title)
865
-
866
- return chart
867
- except Exception as e:
868
- # Create placeholder if chart creation fails
869
- textbox = slide.shapes.add_textbox(
870
- Inches(pos['left']),
871
- Inches(pos['top']),
872
- Inches(pos['width']),
873
- Inches(pos['height'])
874
- )
875
- textbox.text_frame.text = f"Chart: {chart_type}\n{chart_title or 'Chart Placeholder'}"
876
- return textbox
877
-
878
-
879
- def apply_slide_background(slide, background_config: Dict, templates_data: Dict, color_scheme: str) -> None:
880
- """
881
- Apply background styling to a slide based on template configuration.
882
-
883
- Args:
884
- slide: PowerPoint slide object
885
- background_config: Background configuration from template
886
- templates_data: Template data dictionary
887
- color_scheme: Selected color scheme
888
- """
889
- if not background_config:
890
- return
891
-
892
- bg_type = background_config.get('type', 'solid')
893
-
894
- if bg_type == 'professional_gradient':
895
- style = background_config.get('style', 'subtle')
896
- direction = background_config.get('direction', 'diagonal')
897
- design_utils.create_professional_gradient_background(slide, color_scheme, style, direction)
898
- elif bg_type == 'solid':
899
- color_role = background_config.get('color_role', 'light')
900
- # Note: Solid background would require XML manipulation for proper implementation
901
- pass
902
-
903
-
904
-
905
-
906
- def apply_slide_template_basic(slide, template_id: str, color_scheme: str = 'modern_blue',
907
- content_mapping: Dict = None, image_paths: Dict = None) -> Dict:
908
- """
909
- Apply a basic slide template to create a formatted slide.
910
-
911
- Args:
912
- slide: PowerPoint slide object
913
- template_id: ID of the template to apply
914
- color_scheme: Color scheme to use
915
- content_mapping: Dictionary mapping element roles to content
916
- image_paths: Dictionary mapping image element roles to file paths
917
-
918
- Returns:
919
- Dictionary with application results
920
- """
921
- try:
922
- # Load templates
923
- templates_data = load_slide_templates()
924
-
925
- if template_id not in templates_data.get('templates', {}):
926
- return {
927
- 'success': False,
928
- 'error': f"Template '{template_id}' not found"
929
- }
930
-
931
- template = templates_data['templates'][template_id]
932
- elements_created = []
933
-
934
- # Apply background if specified
935
- background_config = template.get('background')
936
- if background_config:
937
- apply_slide_background(slide, background_config, templates_data, color_scheme)
938
-
939
- # Create elements
940
- for element in template.get('elements', []):
941
- element_type = element.get('type')
942
- element_role = element.get('role', '')
943
-
944
- try:
945
- # Override placeholder text with custom content if provided
946
- if content_mapping and element_role in content_mapping:
947
- element = element.copy() # Don't modify original template
948
- element['placeholder_text'] = content_mapping[element_role]
949
-
950
- created_element = None
951
-
952
- if element_type == 'text':
953
- created_element = create_text_element(slide, element, templates_data, color_scheme)
954
- elif element_type == 'image':
955
- image_path = image_paths.get(element_role) if image_paths else None
956
- created_element = create_image_element(slide, element, image_path)
957
- elif element_type == 'shape':
958
- created_element = create_shape_element(slide, element, templates_data, color_scheme)
959
- elif element_type == 'table':
960
- created_element = create_table_element(slide, element, templates_data, color_scheme)
961
- elif element_type == 'chart':
962
- created_element = create_chart_element(slide, element, templates_data, color_scheme)
963
-
964
- if created_element:
965
- elements_created.append({
966
- 'type': element_type,
967
- 'role': element_role,
968
- 'index': len(slide.shapes) - 1
969
- })
970
-
971
- except Exception as e:
972
- # Continue with other elements if one fails
973
- elements_created.append({
974
- 'type': element_type,
975
- 'role': element_role,
976
- 'error': str(e)
977
- })
978
-
979
- return {
980
- 'success': True,
981
- 'template_id': template_id,
982
- 'template_name': template.get('name', template_id),
983
- 'color_scheme': color_scheme,
984
- 'elements_created': elements_created,
985
- 'total_elements': len(template.get('elements', []))
986
- }
987
-
988
- except Exception as e:
989
- return {
990
- 'success': False,
991
- 'error': f"Failed to apply template: {str(e)}"
992
- }
993
-
994
-
995
- def apply_slide_template(slide, template_id: str, color_scheme: str = 'modern_blue',
996
- content_mapping: Dict = None, image_paths: Dict = None) -> Dict:
997
- """
998
- Apply a slide template with all enhanced features.
999
-
1000
- Args:
1001
- slide: PowerPoint slide object
1002
- template_id: ID of the template to apply
1003
- color_scheme: Color scheme to use
1004
- content_mapping: Dictionary mapping element roles to content
1005
- image_paths: Dictionary mapping image element roles to file paths
1006
-
1007
- Returns:
1008
- Dictionary with application results
1009
- """
1010
- # All templates now have enhanced features built-in
1011
- return enhanced_template_manager.apply_enhanced_slide_template(
1012
- slide, template_id, color_scheme, content_mapping, image_paths
1013
- )
1014
-
1015
-
1016
- def create_presentation_from_template_sequence(presentation: Presentation, template_sequence: List[Dict],
1017
- color_scheme: str = 'modern_blue') -> Dict:
1018
- """
1019
- Create a complete presentation from a sequence of templates.
1020
-
1021
- Args:
1022
- presentation: PowerPoint presentation object
1023
- template_sequence: List of template configurations
1024
- color_scheme: Color scheme to apply to all slides
1025
-
1026
- Returns:
1027
- Dictionary with creation results
1028
- """
1029
- results = {
1030
- 'success': True,
1031
- 'slides_created': [],
1032
- 'total_slides': len(template_sequence),
1033
- 'color_scheme': color_scheme
1034
- }
1035
-
1036
- for i, slide_config in enumerate(template_sequence):
1037
- try:
1038
- # Get template configuration
1039
- template_id = slide_config.get('template_id')
1040
- content_mapping = slide_config.get('content', {})
1041
- image_paths = slide_config.get('images', {})
1042
-
1043
- if not template_id:
1044
- results['slides_created'].append({
1045
- 'slide_index': i,
1046
- 'success': False,
1047
- 'error': 'No template_id specified'
1048
- })
1049
- continue
1050
-
1051
- # Add new slide (using layout 1 as default content layout)
1052
- layout = presentation.slide_layouts[1]
1053
- slide = presentation.slides.add_slide(layout)
1054
-
1055
- # Apply template
1056
- template_result = apply_slide_template(
1057
- slide, template_id, color_scheme, content_mapping, image_paths
1058
- )
1059
-
1060
- template_result['slide_index'] = i
1061
- results['slides_created'].append(template_result)
1062
-
1063
- if not template_result['success']:
1064
- results['success'] = False
1065
-
1066
- except Exception as e:
1067
- results['slides_created'].append({
1068
- 'slide_index': i,
1069
- 'success': False,
1070
- 'error': f"Failed to create slide {i}: {str(e)}"
1071
- })
1072
- results['success'] = False
1073
-
1074
- return results
1075
-
1076
-
1077
- def get_template_usage_examples() -> Dict:
1078
- """
1079
- Get examples of how to use different templates.
1080
-
1081
- Returns:
1082
- Dictionary with usage examples
1083
- """
1084
- return {
1085
- "single_slide_example": {
1086
- "description": "Apply a single template to a slide",
1087
- "code": {
1088
- "template_id": "text_with_image",
1089
- "color_scheme": "modern_blue",
1090
- "content_mapping": {
1091
- "title": "Our Solution",
1092
- "content": "• Increased efficiency by 40%\n• Reduced costs significantly\n• Improved user satisfaction",
1093
- },
1094
- "image_paths": {
1095
- "supporting": "/path/to/solution_image.jpg"
1096
- }
1097
- }
1098
- },
1099
- "presentation_sequence_example": {
1100
- "description": "Create a complete presentation from templates",
1101
- "code": [
1102
- {
1103
- "template_id": "title_slide",
1104
- "content": {
1105
- "title": "2024 Business Review",
1106
- "subtitle": "Annual Performance Report",
1107
- "author": "John Smith, CEO"
1108
- }
1109
- },
1110
- {
1111
- "template_id": "agenda_slide",
1112
- "content": {
1113
- "agenda_items": "1. Executive Summary\n\n2. Financial Performance\n\n3. Market Analysis\n\n4. Future Strategy"
1114
- }
1115
- },
1116
- {
1117
- "template_id": "key_metrics_dashboard",
1118
- "content": {
1119
- "metric_1_value": "92%",
1120
- "metric_2_value": "$3.2M",
1121
- "metric_3_value": "340",
1122
- "metric_4_value": "18%"
1123
- }
1124
- },
1125
- {
1126
- "template_id": "thank_you_slide",
1127
- "content": {
1128
- "contact": "Questions?\njohn.smith@company.com\n(555) 123-4567"
1129
- }
1130
- }
1131
- ]
1132
- },
1133
- "available_templates": [
1134
- "title_slide", "text_with_image", "two_column_text", "two_column_text_images",
1135
- "three_column_layout", "agenda_slide", "chapter_intro", "thank_you_slide",
1136
- "timeline_slide", "data_table_slide", "chart_comparison", "full_image_slide",
1137
- "process_flow", "quote_testimonial", "key_metrics_dashboard",
1138
- "before_after_comparison", "team_introduction"
1139
- ],
1140
- "color_schemes": [
1141
- "modern_blue", "corporate_gray", "elegant_green", "warm_red"
1142
- ]
1
+ """
2
+ Enhanced template management utilities for PowerPoint MCP Server.
3
+ Advanced slide creation with dynamic sizing, auto-wrapping, and visual effects.
4
+ Combines features from both basic and enhanced template systems.
5
+ """
6
+ import json
7
+ import os
8
+ import re
9
+ from typing import Dict, List, Optional, Any, Tuple
10
+ from pptx import Presentation
11
+ from pptx.util import Inches, Pt
12
+ from pptx.dml.color import RGBColor
13
+ from pptx.enum.text import PP_ALIGN, MSO_VERTICAL_ANCHOR
14
+ from pptx.enum.shapes import MSO_SHAPE
15
+ import utils.content_utils as content_utils
16
+ import utils.design_utils as design_utils
17
+
18
+
19
+ class TextSizeCalculator:
20
+ """Calculate optimal text sizes based on content and container dimensions."""
21
+
22
+ def __init__(self):
23
+ self.character_widths = {
24
+ 'narrow': 0.6, # i, l, t
25
+ 'normal': 1.0, # most characters
26
+ 'wide': 1.3, # m, w
27
+ 'space': 0.5 # space character
28
+ }
29
+
30
+ def estimate_text_width(self, text: str, font_size: int) -> float:
31
+ """Estimate text width in points based on character analysis."""
32
+ if not text:
33
+ return 0
34
+
35
+ width = 0
36
+ for char in text:
37
+ if char in 'iltj':
38
+ width += self.character_widths['narrow']
39
+ elif char in 'mwMW':
40
+ width += self.character_widths['wide']
41
+ elif char == ' ':
42
+ width += self.character_widths['space']
43
+ else:
44
+ width += self.character_widths['normal']
45
+
46
+ return width * font_size * 0.6 # Approximation factor
47
+
48
+ def estimate_text_height(self, text: str, font_size: int, line_spacing: float = 1.2) -> float:
49
+ """Estimate text height based on line count and spacing."""
50
+ lines = len(text.split('\n'))
51
+ return lines * font_size * line_spacing * 1.3 # Convert to points
52
+
53
+ def calculate_optimal_font_size(self, text: str, container_width: float,
54
+ container_height: float, font_type: str = 'body',
55
+ min_size: int = 8, max_size: int = 36) -> int:
56
+ """Calculate optimal font size to fit text in container."""
57
+ container_width_pts = container_width * 72 # Convert inches to points
58
+ container_height_pts = container_height * 72
59
+
60
+ # Start with a reasonable size and adjust
61
+ for font_size in range(max_size, min_size - 1, -1):
62
+ estimated_width = self.estimate_text_width(text, font_size)
63
+ estimated_height = self.estimate_text_height(text, font_size)
64
+
65
+ if estimated_width <= container_width_pts * 0.9 and estimated_height <= container_height_pts * 0.9:
66
+ return font_size
67
+
68
+ return min_size
69
+
70
+ def wrap_text_intelligently(self, text: str, max_width: float, font_size: int) -> str:
71
+ """Intelligently wrap text to fit within specified width."""
72
+ if not text:
73
+ return text
74
+
75
+ max_width_pts = max_width * 72
76
+ words = text.split()
77
+ wrapped_lines = []
78
+ current_line = []
79
+
80
+ for word in words:
81
+ test_line = current_line + [word]
82
+ test_text = ' '.join(test_line)
83
+
84
+ if self.estimate_text_width(test_text, font_size) <= max_width_pts:
85
+ current_line.append(word)
86
+ else:
87
+ if current_line:
88
+ wrapped_lines.append(' '.join(current_line))
89
+ current_line = [word]
90
+ else:
91
+ # Single word is too long, force wrap
92
+ wrapped_lines.append(word)
93
+
94
+ if current_line:
95
+ wrapped_lines.append(' '.join(current_line))
96
+
97
+ return '\n'.join(wrapped_lines)
98
+
99
+
100
+ class VisualEffectsManager:
101
+ """Manage and apply visual effects to PowerPoint elements."""
102
+
103
+ def __init__(self, templates_data: Dict):
104
+ self.templates_data = templates_data
105
+ self.text_effects = templates_data.get('text_effects', {})
106
+ self.image_effects = templates_data.get('image_effects', {})
107
+
108
+ def apply_text_effects(self, text_frame, effects: List[str], color_scheme: str) -> None:
109
+ """Apply text effects like shadows, glows, and outlines."""
110
+ for effect_name in effects:
111
+ if effect_name not in self.text_effects:
112
+ continue
113
+
114
+ effect_config = self.text_effects[effect_name]
115
+ effect_type = effect_config.get('type')
116
+
117
+ # Note: These are simplified implementations
118
+ # Full implementation would require XML manipulation
119
+ try:
120
+ if effect_type == 'shadow':
121
+ self._apply_text_shadow(text_frame, effect_config, color_scheme)
122
+ elif effect_type == 'glow':
123
+ self._apply_text_glow(text_frame, effect_config, color_scheme)
124
+ elif effect_type == 'outline':
125
+ self._apply_text_outline(text_frame, effect_config, color_scheme)
126
+ except Exception:
127
+ # Graceful fallback if effect application fails
128
+ pass
129
+
130
+ def _apply_text_shadow(self, text_frame, config: Dict, color_scheme: str) -> None:
131
+ """Apply shadow effect to text (simplified implementation)."""
132
+ # In a full implementation, this would manipulate the XML directly
133
+ # For now, we'll apply basic formatting that creates a shadow-like effect
134
+ for paragraph in text_frame.paragraphs:
135
+ for run in paragraph.runs:
136
+ # Make text slightly bolder to simulate shadow depth
137
+ run.font.bold = True
138
+
139
+ def _apply_text_glow(self, text_frame, config: Dict, color_scheme: str) -> None:
140
+ """Apply glow effect to text (simplified implementation)."""
141
+ pass # Would require XML manipulation for true glow effect
142
+
143
+ def _apply_text_outline(self, text_frame, config: Dict, color_scheme: str) -> None:
144
+ """Apply outline effect to text (simplified implementation)."""
145
+ pass # Would require XML manipulation for true outline effect
146
+
147
+ def apply_image_effects(self, image_shape, effect_name: str, color_scheme: str) -> None:
148
+ """Apply visual effects to image shapes."""
149
+ if effect_name not in self.image_effects:
150
+ return
151
+
152
+ effect_config = self.image_effects[effect_name]
153
+
154
+ try:
155
+ # Apply shadow if specified
156
+ if 'shadow' in effect_config:
157
+ shadow_config = effect_config['shadow']
158
+ # Simplified shadow application
159
+ pass
160
+
161
+ # Apply border if specified
162
+ if 'border' in effect_config:
163
+ border_config = effect_config['border']
164
+ if 'width' in border_config:
165
+ image_shape.line.width = Pt(border_config['width'])
166
+ if 'color_role' in border_config:
167
+ color = self._get_color_from_scheme(color_scheme, border_config['color_role'])
168
+ image_shape.line.color.rgb = RGBColor(*color)
169
+ elif 'color' in border_config:
170
+ image_shape.line.color.rgb = RGBColor(*border_config['color'])
171
+
172
+ except Exception:
173
+ # Graceful fallback
174
+ pass
175
+
176
+ def _get_color_from_scheme(self, color_scheme: str, color_role: str) -> Tuple[int, int, int]:
177
+ """Get color from scheme (helper method)."""
178
+ schemes = self.templates_data.get('color_schemes', {})
179
+ if color_scheme in schemes and color_role in schemes[color_scheme]:
180
+ return tuple(schemes[color_scheme][color_role])
181
+ return (0, 0, 0) # Default black
182
+
183
+
184
+ class EnhancedTemplateManager:
185
+ """Enhanced template manager with dynamic features."""
186
+
187
+ def __init__(self, template_file_path: str = None):
188
+ self.text_calculator = TextSizeCalculator()
189
+ self.load_templates(template_file_path)
190
+ self.effects_manager = VisualEffectsManager(self.templates_data)
191
+
192
+ def load_templates(self, template_file_path: str = None) -> None:
193
+ """Load unified templates with all dynamic features."""
194
+ if template_file_path is None:
195
+ current_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
196
+ # Use the unified template file
197
+ template_file_path = os.path.join(current_dir, 'slide_layout_templates.json')
198
+
199
+ try:
200
+ with open(template_file_path, 'r', encoding='utf-8') as f:
201
+ self.templates_data = json.load(f)
202
+ except FileNotFoundError:
203
+ raise FileNotFoundError(f"Template file not found: {template_file_path}")
204
+ except json.JSONDecodeError as e:
205
+ raise ValueError(f"Invalid JSON in template file: {str(e)}")
206
+
207
+
208
+ def get_dynamic_font_size(self, element: Dict, content: str = None) -> int:
209
+ """Calculate dynamic font size based on content and container."""
210
+ content = content or element.get('placeholder_text', '')
211
+ if not content:
212
+ return 14 # Default size
213
+
214
+ # Get container dimensions
215
+ pos = element.get('position', {})
216
+ container_width = pos.get('width', 4.0)
217
+ container_height = pos.get('height', 1.0)
218
+
219
+ # Get font constraints
220
+ font_type = element.get('styling', {}).get('font_type', 'body')
221
+ sizing_rules = self.templates_data.get('auto_sizing_rules', {})
222
+ base_sizes = sizing_rules.get('text_measurement', {}).get('base_font_sizes', {})
223
+
224
+ if font_type in base_sizes:
225
+ min_size = base_sizes[font_type]['min']
226
+ max_size = base_sizes[font_type]['max']
227
+ default_size = base_sizes[font_type]['default']
228
+ else:
229
+ min_size, max_size, default_size = 10, 18, 14
230
+
231
+ # Check if dynamic sizing is requested
232
+ font_size_setting = element.get('styling', {}).get('font_size')
233
+ if font_size_setting == 'dynamic':
234
+ return self.text_calculator.calculate_optimal_font_size(
235
+ content, container_width, container_height, font_type, min_size, max_size
236
+ )
237
+
238
+ return default_size
239
+
240
+ def apply_enhanced_slide_template(self, slide, template_id: str, color_scheme: str = 'modern_blue',
241
+ content_mapping: Dict = None, image_paths: Dict = None) -> Dict:
242
+ """Apply enhanced slide template with all dynamic features."""
243
+ try:
244
+ if template_id not in self.templates_data.get('templates', {}):
245
+ # Fall back to regular template application
246
+ return apply_slide_template_basic(slide, template_id, color_scheme, content_mapping, image_paths)
247
+
248
+ template = self.templates_data['templates'][template_id]
249
+ elements_created = []
250
+
251
+ # Apply enhanced background if specified
252
+ background_config = template.get('background')
253
+ if background_config:
254
+ apply_slide_background(slide, background_config, self.templates_data, color_scheme)
255
+
256
+ # Create enhanced elements
257
+ for element in template.get('elements', []):
258
+ element_type = element.get('type')
259
+ element_role = element.get('role', '')
260
+
261
+ try:
262
+ # Override content if provided
263
+ custom_content = None
264
+ if content_mapping and element_role in content_mapping:
265
+ custom_content = content_mapping[element_role]
266
+
267
+ created_element = None
268
+
269
+ if element_type == 'text':
270
+ created_element = self.create_enhanced_text_element(
271
+ slide, element, self.templates_data, color_scheme, custom_content
272
+ )
273
+ elif element_type == 'shape':
274
+ created_element = create_shape_element(slide, element, self.templates_data, color_scheme)
275
+ elif element_type == 'image':
276
+ image_path = image_paths.get(element_role) if image_paths else None
277
+ created_element = create_image_element(slide, element, image_path)
278
+ elif element_type == 'table':
279
+ created_element = create_table_element(slide, element, self.templates_data, color_scheme)
280
+ elif element_type == 'chart':
281
+ created_element = create_chart_element(slide, element, self.templates_data, color_scheme)
282
+
283
+ if created_element:
284
+ elements_created.append({
285
+ 'type': element_type,
286
+ 'role': element_role,
287
+ 'index': len(slide.shapes) - 1,
288
+ 'enhanced_features': self.get_element_features(element)
289
+ })
290
+
291
+ except Exception as e:
292
+ elements_created.append({
293
+ 'type': element_type,
294
+ 'role': element_role,
295
+ 'error': str(e)
296
+ })
297
+
298
+ return {
299
+ 'success': True,
300
+ 'template_id': template_id,
301
+ 'template_name': template.get('name', template_id),
302
+ 'color_scheme': color_scheme,
303
+ 'elements_created': elements_created,
304
+ 'enhanced_features_applied': [
305
+ 'Dynamic text sizing',
306
+ 'Automatic text wrapping',
307
+ 'Visual effects',
308
+ 'Intelligent content adaptation'
309
+ ]
310
+ }
311
+
312
+ except Exception as e:
313
+ return {
314
+ 'success': False,
315
+ 'error': f"Failed to apply enhanced template: {str(e)}"
316
+ }
317
+
318
+ def create_enhanced_text_element(self, slide, element: Dict, templates_data: Dict,
319
+ color_scheme: str, custom_content: str = None) -> Any:
320
+ """Create text element with enhanced features."""
321
+ pos = element['position']
322
+
323
+ # Determine content
324
+ content = custom_content or element.get('placeholder_text', '')
325
+
326
+ # Apply auto-wrapping if enabled
327
+ styling = element.get('styling', {})
328
+ if styling.get('auto_wrap', False):
329
+ container_width = pos.get('width', 4.0)
330
+ font_size = self.get_dynamic_font_size(element, content)
331
+ content = self.text_calculator.wrap_text_intelligently(content, container_width, font_size)
332
+
333
+ # Create text box
334
+ textbox = slide.shapes.add_textbox(
335
+ Inches(pos['left']),
336
+ Inches(pos['top']),
337
+ Inches(pos['width']),
338
+ Inches(pos['height'])
339
+ )
340
+
341
+ textbox.text_frame.text = content
342
+ textbox.text_frame.word_wrap = True
343
+
344
+ # Apply dynamic font sizing
345
+ font_size = self.get_dynamic_font_size(element, content)
346
+
347
+ # Apply enhanced styling
348
+ self.apply_enhanced_text_styling(textbox.text_frame, element, templates_data, color_scheme, font_size)
349
+
350
+ # Apply auto-fit if enabled
351
+ if styling.get('auto_fit', False):
352
+ textbox.text_frame.auto_size = True
353
+
354
+ return textbox
355
+
356
+ def apply_enhanced_text_styling(self, text_frame, element: Dict, templates_data: Dict,
357
+ color_scheme: str, font_size: int) -> None:
358
+ """Apply enhanced text styling with effects and dynamic features."""
359
+ styling = element.get('styling', {})
360
+
361
+ # Get typography style
362
+ typography_style = templates_data.get('typography_styles', {}).get('modern_sans', {})
363
+ font_type = styling.get('font_type', 'body')
364
+ font_config = typography_style.get(font_type, {'name': 'Segoe UI', 'weight': 'normal'})
365
+
366
+ # Color handling
367
+ color = None
368
+ if 'color_role' in styling:
369
+ color = get_color_from_scheme(templates_data, color_scheme, styling['color_role'])
370
+ elif 'color' in styling:
371
+ color = tuple(styling['color'])
372
+
373
+ # Alignment mapping
374
+ alignment_map = {
375
+ 'left': PP_ALIGN.LEFT,
376
+ 'center': PP_ALIGN.CENTER,
377
+ 'right': PP_ALIGN.RIGHT,
378
+ 'justify': PP_ALIGN.JUSTIFY
379
+ }
380
+
381
+ # Vertical alignment mapping
382
+ vertical_alignment_map = {
383
+ 'top': MSO_VERTICAL_ANCHOR.TOP,
384
+ 'middle': MSO_VERTICAL_ANCHOR.MIDDLE,
385
+ 'bottom': MSO_VERTICAL_ANCHOR.BOTTOM
386
+ }
387
+
388
+ # Apply vertical alignment to text frame
389
+ if 'vertical_alignment' in styling:
390
+ v_align = styling['vertical_alignment']
391
+ if v_align in vertical_alignment_map:
392
+ text_frame.vertical_anchor = vertical_alignment_map[v_align]
393
+
394
+ # Dynamic line spacing
395
+ line_spacing = styling.get('line_spacing', 1.2)
396
+ if line_spacing == 'dynamic':
397
+ content_length = len(text_frame.text)
398
+ if content_length > 300:
399
+ line_spacing = 1.4
400
+ elif content_length > 150:
401
+ line_spacing = 1.3
402
+ else:
403
+ line_spacing = 1.2
404
+
405
+ # Apply formatting to paragraphs and runs
406
+ for paragraph in text_frame.paragraphs:
407
+ # Set alignment
408
+ if 'alignment' in styling and styling['alignment'] in alignment_map:
409
+ paragraph.alignment = alignment_map[styling['alignment']]
410
+
411
+ # Set line spacing
412
+ paragraph.line_spacing = line_spacing
413
+
414
+ # Apply formatting to runs
415
+ for run in paragraph.runs:
416
+ font = run.font
417
+
418
+ # Font family and size
419
+ font.name = font_config['name']
420
+ font.size = Pt(font_size)
421
+
422
+ # Font weight and style
423
+ weight = font_config.get('weight', 'normal')
424
+ font.bold = styling.get('bold', weight in ['bold', 'semibold'])
425
+ font.italic = styling.get('italic', font_config.get('style') == 'italic')
426
+ font.underline = styling.get('underline', False)
427
+
428
+ # Color
429
+ if color:
430
+ font.color.rgb = RGBColor(*color)
431
+
432
+ # Apply text effects
433
+ text_effects = styling.get('text_effects', [])
434
+ if text_effects:
435
+ self.effects_manager.apply_text_effects(text_frame, text_effects, color_scheme)
436
+
437
+ def get_element_features(self, element: Dict) -> List[str]:
438
+ """Get list of enhanced features applied to an element."""
439
+ features = []
440
+ styling = element.get('styling', {})
441
+
442
+ if styling.get('font_size') == 'dynamic':
443
+ features.append('Dynamic text sizing')
444
+ if styling.get('auto_wrap'):
445
+ features.append('Automatic text wrapping')
446
+ if styling.get('text_effects'):
447
+ features.append('Text visual effects')
448
+ if styling.get('auto_fit'):
449
+ features.append('Auto-fit content')
450
+ if 'fill_gradient' in styling:
451
+ features.append('Gradient fills')
452
+ if styling.get('shadow') or styling.get('glow'):
453
+ features.append('Advanced visual effects')
454
+
455
+ return features
456
+
457
+
458
+ # Global instance for enhanced features
459
+ enhanced_template_manager = EnhancedTemplateManager()
460
+
461
+
462
+ def get_enhanced_template_manager() -> EnhancedTemplateManager:
463
+ """Get the global enhanced template manager instance."""
464
+ return enhanced_template_manager
465
+
466
+
467
+ def calculate_dynamic_font_size(text: str, container_width: float, container_height: float,
468
+ font_type: str = 'body') -> int:
469
+ """Calculate optimal font size for given text and container."""
470
+ return enhanced_template_manager.text_calculator.calculate_optimal_font_size(
471
+ text, container_width, container_height, font_type
472
+ )
473
+
474
+
475
+ def wrap_text_automatically(text: str, container_width: float, font_size: int) -> str:
476
+ """Automatically wrap text to fit container width."""
477
+ return enhanced_template_manager.text_calculator.wrap_text_intelligently(
478
+ text, container_width, font_size
479
+ )
480
+
481
+
482
+ def load_slide_templates(template_file_path: str = None) -> Dict:
483
+ """
484
+ Load slide layout templates from JSON file.
485
+
486
+ Args:
487
+ template_file_path: Path to template JSON file (defaults to slide_layout_templates.json)
488
+
489
+ Returns:
490
+ Dictionary containing all template definitions
491
+ """
492
+ if template_file_path is None:
493
+ # Default to the template file in the same directory as the script
494
+ current_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
495
+ template_file_path = os.path.join(current_dir, 'slide_layout_templates.json')
496
+
497
+ try:
498
+ with open(template_file_path, 'r', encoding='utf-8') as f:
499
+ templates = json.load(f)
500
+ return templates
501
+ except FileNotFoundError:
502
+ raise FileNotFoundError(f"Template file not found: {template_file_path}")
503
+ except json.JSONDecodeError as e:
504
+ raise ValueError(f"Invalid JSON in template file: {str(e)}")
505
+
506
+
507
+ def get_available_templates() -> List[Dict]:
508
+ """
509
+ Get a list of all available slide templates.
510
+
511
+ Returns:
512
+ List of template information dictionaries
513
+ """
514
+ try:
515
+ templates_data = load_slide_templates()
516
+ template_list = []
517
+
518
+ for template_id, template_info in templates_data.get('templates', {}).items():
519
+ template_list.append({
520
+ 'id': template_id,
521
+ 'name': template_info.get('name', template_id),
522
+ 'description': template_info.get('description', ''),
523
+ 'layout_type': template_info.get('layout_type', 'content'),
524
+ 'element_count': len(template_info.get('elements', []))
525
+ })
526
+
527
+ return template_list
528
+ except Exception as e:
529
+ return [{'error': f"Failed to load templates: {str(e)}"}]
530
+
531
+
532
+ def get_color_from_scheme(templates_data: Dict, color_scheme: str, color_role: str) -> Tuple[int, int, int]:
533
+ """
534
+ Get RGB color values from a color scheme.
535
+
536
+ Args:
537
+ templates_data: Template data dictionary
538
+ color_scheme: Name of the color scheme
539
+ color_role: Role of the color (primary, secondary, accent1, etc.)
540
+
541
+ Returns:
542
+ RGB color tuple (r, g, b)
543
+ """
544
+ color_schemes = templates_data.get('color_schemes', {})
545
+
546
+ if color_scheme not in color_schemes:
547
+ color_scheme = 'modern_blue' # Default fallback
548
+
549
+ scheme = color_schemes[color_scheme]
550
+ return tuple(scheme.get(color_role, scheme.get('primary', [0, 120, 215])))
551
+
552
+
553
+ def get_font_settings(templates_data: Dict, font_type: str, font_size: str) -> Dict:
554
+ """
555
+ Get font settings from typography configuration.
556
+
557
+ Args:
558
+ templates_data: Template data dictionary
559
+ font_type: Type of font (title, subtitle, body, caption)
560
+ font_size: Size category (large, medium, small)
561
+
562
+ Returns:
563
+ Dictionary with font settings
564
+ """
565
+ typography = templates_data.get('typography', {})
566
+
567
+ if font_type not in typography:
568
+ font_type = 'body' # Default fallback
569
+
570
+ font_config = typography[font_type]
571
+ size_key = f'font_size_{font_size}'
572
+
573
+ return {
574
+ 'name': font_config.get('font_name', 'Segoe UI'),
575
+ 'size': font_config.get(size_key, font_config.get('font_size_medium', 14)),
576
+ 'bold': font_config.get('bold', False)
577
+ }
578
+
579
+
580
+ def apply_text_styling(text_frame, styling: Dict, templates_data: Dict, color_scheme: str) -> None:
581
+ """
582
+ Apply text styling based on template configuration.
583
+
584
+ Args:
585
+ text_frame: PowerPoint text frame object
586
+ styling: Styling configuration from template
587
+ templates_data: Template data dictionary
588
+ color_scheme: Selected color scheme
589
+ """
590
+ # Get font settings
591
+ font_type = styling.get('font_type', 'body')
592
+ font_size_category = styling.get('font_size', 'medium')
593
+ font_settings = get_font_settings(templates_data, font_type, font_size_category)
594
+
595
+ # Get color
596
+ color = None
597
+ if 'color_role' in styling:
598
+ color = get_color_from_scheme(templates_data, color_scheme, styling['color_role'])
599
+ elif 'color' in styling:
600
+ color = tuple(styling['color'])
601
+
602
+ # Apply alignment
603
+ alignment_map = {
604
+ 'left': PP_ALIGN.LEFT,
605
+ 'center': PP_ALIGN.CENTER,
606
+ 'right': PP_ALIGN.RIGHT,
607
+ 'justify': PP_ALIGN.JUSTIFY
608
+ }
609
+
610
+ # Apply formatting to all paragraphs and runs
611
+ for paragraph in text_frame.paragraphs:
612
+ if 'alignment' in styling and styling['alignment'] in alignment_map:
613
+ paragraph.alignment = alignment_map[styling['alignment']]
614
+
615
+ for run in paragraph.runs:
616
+ font = run.font
617
+ font.name = font_settings['name']
618
+ font.size = Pt(font_settings['size'])
619
+ font.bold = styling.get('bold', font_settings['bold'])
620
+ font.italic = styling.get('italic', False)
621
+ font.underline = styling.get('underline', False)
622
+
623
+ if color:
624
+ font.color.rgb = RGBColor(*color)
625
+
626
+
627
+ def create_text_element(slide, element: Dict, templates_data: Dict, color_scheme: str) -> Any:
628
+ """
629
+ Create a text element on a slide based on template configuration.
630
+
631
+ Args:
632
+ slide: PowerPoint slide object
633
+ element: Element configuration from template
634
+ templates_data: Template data dictionary
635
+ color_scheme: Selected color scheme
636
+
637
+ Returns:
638
+ Created text box shape
639
+ """
640
+ pos = element['position']
641
+ textbox = slide.shapes.add_textbox(
642
+ Inches(pos['left']),
643
+ Inches(pos['top']),
644
+ Inches(pos['width']),
645
+ Inches(pos['height'])
646
+ )
647
+
648
+ # Set text content
649
+ textbox.text_frame.text = element.get('placeholder_text', '')
650
+
651
+ # Apply styling
652
+ styling = element.get('styling', {})
653
+ apply_text_styling(textbox.text_frame, styling, templates_data, color_scheme)
654
+
655
+ return textbox
656
+
657
+
658
+ def create_image_element(slide, element: Dict, image_path: str = None) -> Any:
659
+ """
660
+ Create an image element on a slide based on template configuration.
661
+
662
+ Args:
663
+ slide: PowerPoint slide object
664
+ element: Element configuration from template
665
+ image_path: Optional path to image file
666
+
667
+ Returns:
668
+ Created image shape or None if no image provided
669
+ """
670
+ if not image_path:
671
+ # Create placeholder rectangle if no image provided
672
+ pos = element['position']
673
+ placeholder = slide.shapes.add_shape(
674
+ 1, # Rectangle shape
675
+ Inches(pos['left']),
676
+ Inches(pos['top']),
677
+ Inches(pos['width']),
678
+ Inches(pos['height'])
679
+ )
680
+
681
+ # Add placeholder text
682
+ if hasattr(placeholder, 'text_frame'):
683
+ placeholder.text_frame.text = element.get('placeholder_text', 'Image Placeholder')
684
+
685
+ return placeholder
686
+
687
+ try:
688
+ pos = element['position']
689
+ image_shape = content_utils.add_image(
690
+ slide,
691
+ image_path,
692
+ pos['left'],
693
+ pos['top'],
694
+ pos['width'],
695
+ pos['height']
696
+ )
697
+
698
+ # Apply styling if specified
699
+ styling = element.get('styling', {})
700
+ if styling.get('shadow'):
701
+ # Apply shadow effect (simplified)
702
+ pass
703
+
704
+ return image_shape
705
+ except Exception:
706
+ # Fallback to placeholder if image fails to load
707
+ return create_image_element(slide, element, None)
708
+
709
+
710
+ def create_shape_element(slide, element: Dict, templates_data: Dict, color_scheme: str) -> Any:
711
+ """
712
+ Create a shape element on a slide based on template configuration.
713
+
714
+ Args:
715
+ slide: PowerPoint slide object
716
+ element: Element configuration from template
717
+ templates_data: Template data dictionary
718
+ color_scheme: Selected color scheme
719
+
720
+ Returns:
721
+ Created shape
722
+ """
723
+ pos = element['position']
724
+ shape_type = element.get('shape_type', 'rectangle')
725
+
726
+ try:
727
+ # Import the shape creation function from the main server
728
+ from ppt_mcp_server import add_shape_direct
729
+ shape = add_shape_direct(slide, shape_type, pos['left'], pos['top'], pos['width'], pos['height'])
730
+
731
+ # Apply styling
732
+ styling = element.get('styling', {})
733
+
734
+ # Fill color
735
+ if 'fill_color_role' in styling:
736
+ fill_color = get_color_from_scheme(templates_data, color_scheme, styling['fill_color_role'])
737
+ shape.fill.solid()
738
+ shape.fill.fore_color.rgb = RGBColor(*fill_color)
739
+ elif 'fill_color' in styling:
740
+ shape.fill.solid()
741
+ shape.fill.fore_color.rgb = RGBColor(*styling['fill_color'])
742
+
743
+ # Line color
744
+ if 'line_color_role' in styling:
745
+ line_color = get_color_from_scheme(templates_data, color_scheme, styling['line_color_role'])
746
+ shape.line.color.rgb = RGBColor(*line_color)
747
+ elif styling.get('no_border'):
748
+ shape.line.fill.background()
749
+
750
+ # Transparency
751
+ if 'transparency' in styling:
752
+ # Note: Transparency implementation would need additional XML manipulation
753
+ pass
754
+
755
+ return shape
756
+ except Exception as e:
757
+ # Create a simple rectangle as fallback
758
+ textbox = slide.shapes.add_textbox(
759
+ Inches(pos['left']),
760
+ Inches(pos['top']),
761
+ Inches(pos['width']),
762
+ Inches(pos['height'])
763
+ )
764
+ textbox.text_frame.text = f"Shape: {shape_type}"
765
+ return textbox
766
+
767
+
768
+ def create_table_element(slide, element: Dict, templates_data: Dict, color_scheme: str) -> Any:
769
+ """
770
+ Create a table element on a slide based on template configuration.
771
+
772
+ Args:
773
+ slide: PowerPoint slide object
774
+ element: Element configuration from template
775
+ templates_data: Template data dictionary
776
+ color_scheme: Selected color scheme
777
+
778
+ Returns:
779
+ Created table shape
780
+ """
781
+ pos = element['position']
782
+ table_config = element.get('table_config', {})
783
+
784
+ rows = table_config.get('rows', 3)
785
+ cols = table_config.get('cols', 3)
786
+
787
+ # Create table
788
+ table_shape = content_utils.add_table(
789
+ slide, rows, cols, pos['left'], pos['top'], pos['width'], pos['height']
790
+ )
791
+ table = table_shape.table
792
+
793
+ # Populate with data if provided
794
+ data = table_config.get('data', [])
795
+ for r in range(min(rows, len(data))):
796
+ for c in range(min(cols, len(data[r]))):
797
+ table.cell(r, c).text = str(data[r][c])
798
+
799
+ # Apply styling
800
+ styling = element.get('styling', {})
801
+ header_row = table_config.get('header_row', True)
802
+
803
+ for r in range(rows):
804
+ for c in range(cols):
805
+ cell = table.cell(r, c)
806
+
807
+ if r == 0 and header_row:
808
+ # Header styling
809
+ if 'header_bg_color_role' in styling:
810
+ bg_color = get_color_from_scheme(templates_data, color_scheme, styling['header_bg_color_role'])
811
+ cell.fill.solid()
812
+ cell.fill.fore_color.rgb = RGBColor(*bg_color)
813
+
814
+ # Header text color
815
+ if 'header_text_color' in styling:
816
+ for paragraph in cell.text_frame.paragraphs:
817
+ for run in paragraph.runs:
818
+ run.font.color.rgb = RGBColor(*styling['header_text_color'])
819
+ run.font.bold = True
820
+ else:
821
+ # Body styling
822
+ if 'body_bg_color_role' in styling:
823
+ bg_color = get_color_from_scheme(templates_data, color_scheme, styling['body_bg_color_role'])
824
+ cell.fill.solid()
825
+ cell.fill.fore_color.rgb = RGBColor(*bg_color)
826
+
827
+ return table_shape
828
+
829
+
830
+ def create_chart_element(slide, element: Dict, templates_data: Dict, color_scheme: str) -> Any:
831
+ """
832
+ Create a chart element on a slide based on template configuration.
833
+
834
+ Args:
835
+ slide: PowerPoint slide object
836
+ element: Element configuration from template
837
+ templates_data: Template data dictionary
838
+ color_scheme: Selected color scheme
839
+
840
+ Returns:
841
+ Created chart object
842
+ """
843
+ pos = element['position']
844
+ chart_config = element.get('chart_config', {})
845
+
846
+ chart_type = chart_config.get('type', 'column')
847
+ categories = chart_config.get('categories', ['A', 'B', 'C'])
848
+ series_data = chart_config.get('series', [{'name': 'Series 1', 'values': [1, 2, 3]}])
849
+
850
+ # Extract series names and values
851
+ series_names = [s['name'] for s in series_data]
852
+ series_values = [s['values'] for s in series_data]
853
+
854
+ try:
855
+ # Create chart
856
+ chart = content_utils.add_chart(
857
+ slide, chart_type, pos['left'], pos['top'], pos['width'], pos['height'],
858
+ categories, series_names, series_values
859
+ )
860
+
861
+ # Apply formatting
862
+ chart_title = chart_config.get('title')
863
+ if chart_title:
864
+ content_utils.format_chart(chart, title=chart_title)
865
+
866
+ return chart
867
+ except Exception as e:
868
+ # Create placeholder if chart creation fails
869
+ textbox = slide.shapes.add_textbox(
870
+ Inches(pos['left']),
871
+ Inches(pos['top']),
872
+ Inches(pos['width']),
873
+ Inches(pos['height'])
874
+ )
875
+ textbox.text_frame.text = f"Chart: {chart_type}\n{chart_title or 'Chart Placeholder'}"
876
+ return textbox
877
+
878
+
879
+ def apply_slide_background(slide, background_config: Dict, templates_data: Dict, color_scheme: str) -> None:
880
+ """
881
+ Apply background styling to a slide based on template configuration.
882
+
883
+ Args:
884
+ slide: PowerPoint slide object
885
+ background_config: Background configuration from template
886
+ templates_data: Template data dictionary
887
+ color_scheme: Selected color scheme
888
+ """
889
+ if not background_config:
890
+ return
891
+
892
+ bg_type = background_config.get('type', 'solid')
893
+
894
+ if bg_type == 'professional_gradient':
895
+ style = background_config.get('style', 'subtle')
896
+ direction = background_config.get('direction', 'diagonal')
897
+ design_utils.create_professional_gradient_background(slide, color_scheme, style, direction)
898
+ elif bg_type == 'solid':
899
+ color_role = background_config.get('color_role', 'light')
900
+ # Note: Solid background would require XML manipulation for proper implementation
901
+ pass
902
+
903
+
904
+
905
+
906
+ def apply_slide_template_basic(slide, template_id: str, color_scheme: str = 'modern_blue',
907
+ content_mapping: Dict = None, image_paths: Dict = None) -> Dict:
908
+ """
909
+ Apply a basic slide template to create a formatted slide.
910
+
911
+ Args:
912
+ slide: PowerPoint slide object
913
+ template_id: ID of the template to apply
914
+ color_scheme: Color scheme to use
915
+ content_mapping: Dictionary mapping element roles to content
916
+ image_paths: Dictionary mapping image element roles to file paths
917
+
918
+ Returns:
919
+ Dictionary with application results
920
+ """
921
+ try:
922
+ # Load templates
923
+ templates_data = load_slide_templates()
924
+
925
+ if template_id not in templates_data.get('templates', {}):
926
+ return {
927
+ 'success': False,
928
+ 'error': f"Template '{template_id}' not found"
929
+ }
930
+
931
+ template = templates_data['templates'][template_id]
932
+ elements_created = []
933
+
934
+ # Apply background if specified
935
+ background_config = template.get('background')
936
+ if background_config:
937
+ apply_slide_background(slide, background_config, templates_data, color_scheme)
938
+
939
+ # Create elements
940
+ for element in template.get('elements', []):
941
+ element_type = element.get('type')
942
+ element_role = element.get('role', '')
943
+
944
+ try:
945
+ # Override placeholder text with custom content if provided
946
+ if content_mapping and element_role in content_mapping:
947
+ element = element.copy() # Don't modify original template
948
+ element['placeholder_text'] = content_mapping[element_role]
949
+
950
+ created_element = None
951
+
952
+ if element_type == 'text':
953
+ created_element = create_text_element(slide, element, templates_data, color_scheme)
954
+ elif element_type == 'image':
955
+ image_path = image_paths.get(element_role) if image_paths else None
956
+ created_element = create_image_element(slide, element, image_path)
957
+ elif element_type == 'shape':
958
+ created_element = create_shape_element(slide, element, templates_data, color_scheme)
959
+ elif element_type == 'table':
960
+ created_element = create_table_element(slide, element, templates_data, color_scheme)
961
+ elif element_type == 'chart':
962
+ created_element = create_chart_element(slide, element, templates_data, color_scheme)
963
+
964
+ if created_element:
965
+ elements_created.append({
966
+ 'type': element_type,
967
+ 'role': element_role,
968
+ 'index': len(slide.shapes) - 1
969
+ })
970
+
971
+ except Exception as e:
972
+ # Continue with other elements if one fails
973
+ elements_created.append({
974
+ 'type': element_type,
975
+ 'role': element_role,
976
+ 'error': str(e)
977
+ })
978
+
979
+ return {
980
+ 'success': True,
981
+ 'template_id': template_id,
982
+ 'template_name': template.get('name', template_id),
983
+ 'color_scheme': color_scheme,
984
+ 'elements_created': elements_created,
985
+ 'total_elements': len(template.get('elements', []))
986
+ }
987
+
988
+ except Exception as e:
989
+ return {
990
+ 'success': False,
991
+ 'error': f"Failed to apply template: {str(e)}"
992
+ }
993
+
994
+
995
+ def apply_slide_template(slide, template_id: str, color_scheme: str = 'modern_blue',
996
+ content_mapping: Dict = None, image_paths: Dict = None) -> Dict:
997
+ """
998
+ Apply a slide template with all enhanced features.
999
+
1000
+ Args:
1001
+ slide: PowerPoint slide object
1002
+ template_id: ID of the template to apply
1003
+ color_scheme: Color scheme to use
1004
+ content_mapping: Dictionary mapping element roles to content
1005
+ image_paths: Dictionary mapping image element roles to file paths
1006
+
1007
+ Returns:
1008
+ Dictionary with application results
1009
+ """
1010
+ # All templates now have enhanced features built-in
1011
+ return enhanced_template_manager.apply_enhanced_slide_template(
1012
+ slide, template_id, color_scheme, content_mapping, image_paths
1013
+ )
1014
+
1015
+
1016
+ def create_presentation_from_template_sequence(presentation: Presentation, template_sequence: List[Dict],
1017
+ color_scheme: str = 'modern_blue') -> Dict:
1018
+ """
1019
+ Create a complete presentation from a sequence of templates.
1020
+
1021
+ Args:
1022
+ presentation: PowerPoint presentation object
1023
+ template_sequence: List of template configurations
1024
+ color_scheme: Color scheme to apply to all slides
1025
+
1026
+ Returns:
1027
+ Dictionary with creation results
1028
+ """
1029
+ results = {
1030
+ 'success': True,
1031
+ 'slides_created': [],
1032
+ 'total_slides': len(template_sequence),
1033
+ 'color_scheme': color_scheme
1034
+ }
1035
+
1036
+ for i, slide_config in enumerate(template_sequence):
1037
+ try:
1038
+ # Get template configuration
1039
+ template_id = slide_config.get('template_id')
1040
+ content_mapping = slide_config.get('content', {})
1041
+ image_paths = slide_config.get('images', {})
1042
+
1043
+ if not template_id:
1044
+ results['slides_created'].append({
1045
+ 'slide_index': i,
1046
+ 'success': False,
1047
+ 'error': 'No template_id specified'
1048
+ })
1049
+ continue
1050
+
1051
+ # Add new slide (using layout 1 as default content layout)
1052
+ layout = presentation.slide_layouts[1]
1053
+ slide = presentation.slides.add_slide(layout)
1054
+
1055
+ # Apply template
1056
+ template_result = apply_slide_template(
1057
+ slide, template_id, color_scheme, content_mapping, image_paths
1058
+ )
1059
+
1060
+ template_result['slide_index'] = i
1061
+ results['slides_created'].append(template_result)
1062
+
1063
+ if not template_result['success']:
1064
+ results['success'] = False
1065
+
1066
+ except Exception as e:
1067
+ results['slides_created'].append({
1068
+ 'slide_index': i,
1069
+ 'success': False,
1070
+ 'error': f"Failed to create slide {i}: {str(e)}"
1071
+ })
1072
+ results['success'] = False
1073
+
1074
+ return results
1075
+
1076
+
1077
+ def get_template_usage_examples() -> Dict:
1078
+ """
1079
+ Get examples of how to use different templates.
1080
+
1081
+ Returns:
1082
+ Dictionary with usage examples
1083
+ """
1084
+ return {
1085
+ "single_slide_example": {
1086
+ "description": "Apply a single template to a slide",
1087
+ "code": {
1088
+ "template_id": "text_with_image",
1089
+ "color_scheme": "modern_blue",
1090
+ "content_mapping": {
1091
+ "title": "Our Solution",
1092
+ "content": "• Increased efficiency by 40%\n• Reduced costs significantly\n• Improved user satisfaction",
1093
+ },
1094
+ "image_paths": {
1095
+ "supporting": "/path/to/solution_image.jpg"
1096
+ }
1097
+ }
1098
+ },
1099
+ "presentation_sequence_example": {
1100
+ "description": "Create a complete presentation from templates",
1101
+ "code": [
1102
+ {
1103
+ "template_id": "title_slide",
1104
+ "content": {
1105
+ "title": "2024 Business Review",
1106
+ "subtitle": "Annual Performance Report",
1107
+ "author": "John Smith, CEO"
1108
+ }
1109
+ },
1110
+ {
1111
+ "template_id": "agenda_slide",
1112
+ "content": {
1113
+ "agenda_items": "1. Executive Summary\n\n2. Financial Performance\n\n3. Market Analysis\n\n4. Future Strategy"
1114
+ }
1115
+ },
1116
+ {
1117
+ "template_id": "key_metrics_dashboard",
1118
+ "content": {
1119
+ "metric_1_value": "92%",
1120
+ "metric_2_value": "$3.2M",
1121
+ "metric_3_value": "340",
1122
+ "metric_4_value": "18%"
1123
+ }
1124
+ },
1125
+ {
1126
+ "template_id": "thank_you_slide",
1127
+ "content": {
1128
+ "contact": "Questions?\njohn.smith@company.com\n(555) 123-4567"
1129
+ }
1130
+ }
1131
+ ]
1132
+ },
1133
+ "available_templates": [
1134
+ "title_slide", "text_with_image", "two_column_text", "two_column_text_images",
1135
+ "three_column_layout", "agenda_slide", "chapter_intro", "thank_you_slide",
1136
+ "timeline_slide", "data_table_slide", "chart_comparison", "full_image_slide",
1137
+ "process_flow", "quote_testimonial", "key_metrics_dashboard",
1138
+ "before_after_comparison", "team_introduction"
1139
+ ],
1140
+ "color_schemes": [
1141
+ "modern_blue", "corporate_gray", "elegant_green", "warm_red"
1142
+ ]
1143
1143
  }