1212from sdpm .utils .io import write_json
1313from sdpm .schema .defaults import sort_element_keys
1414
15- def _is_wsl ():
16- try :
17- return 'microsoft' in open ('/proc/version' ).read ().lower ()
18- except Exception :
19- return False
20-
2115def _refresh_autofit_pptx (pptx_path ):
22- """Open PPTX in PowerPoint, apply 'resize shape to fit text' on spAutoFit textboxes, return temp path."""
23- import shutil
24- import subprocess
25- import tempfile
26- from pptx import Presentation as _Prs
27- from pptx .oxml .ns import qn as _qn
28-
29- # Find textboxes (txBox="1") to apply resize
30- prs = _Prs (str (pptx_path ))
31- targets = [] # list of (slide_idx_1based, shape_idx_1based)
32- for si , slide in enumerate (prs .slides , 1 ):
33- for shi , shape in enumerate (slide .shapes , 1 ):
34- sp = shape ._element
35- nvSpPr = sp .find (_qn ('p:nvSpPr' ))
36- if nvSpPr is None :
37- continue
38- cNvSpPr = nvSpPr .find (_qn ('p:cNvSpPr' ))
39- if cNvSpPr is None or cNvSpPr .get ('txBox' ) != '1' :
40- continue
41- targets .append ((si , shi ))
42-
43- if not targets :
44- return None # nothing to refresh
45-
46- tmp_dir = tempfile .mkdtemp ()
47- tmp_pptx = Path (tmp_dir ) / pptx_path .name
48- shutil .copy2 (pptx_path , tmp_pptx )
49-
50- if sys .platform == "darwin" :
51- from sdpm .preview import _mac_open_pptx_background , _mac_restore_pptx_focus_async
52- # Build AppleScript lines for each target shape
53- resize_lines = "\n " .join (
54- f' try\n '
55- f' tell text frame of shape { shi } of slide { si } of pres\n '
56- f' set auto size to shape to fit text\n '
57- f' end tell\n '
58- f' end try'
59- for si , shi in targets
60- )
61- pptx_name = tmp_pptx .name
62- restore_info = _mac_open_pptx_background (tmp_pptx )
63- _mac_restore_pptx_focus_async (restore_info )
64- import time
65- time .sleep (2 )
66- script = f'''
67- tell application "Microsoft PowerPoint"
68- set pres to presentation "{ pptx_name } "
69- { resize_lines }
70- delay 1
71- save pres
72- close pres
73- end tell
74- '''
75- cmd = ["osascript" , "-e" , script ]
76- elif sys .platform == "win32" or _is_wsl ():
77- # PowerShell COM: TextFrame.AutoSize = 1 (ppAutoSizeShapeToFitText)
78- # targets is list of (slide_1based, shape_1based)
79- resize_lines = "; " .join (
80- f"$prs.Slides[{ si } ].Shapes[{ shi } ].TextFrame.AutoSize = 1"
81- for si , shi in targets
82- )
83- ps_cmd = (
84- f"$app = New-Object -ComObject PowerPoint.Application; "
85- f"$prs = $app.Presentations.Open('{ tmp_pptx } '); "
86- f"{ resize_lines } ; "
87- f"$prs.Save(); $prs.Close(); $app.Quit()"
88- )
89- if _is_wsl ():
90- win_path = subprocess .run (["wslpath" , "-w" , str (tmp_pptx )], capture_output = True , text = True ).stdout .strip () # nosec B603 # nosemgrep: python.lang.security.audit.dangerous-subprocess-use-audit
91- ps_cmd = ps_cmd .replace (str (tmp_pptx ), win_path )
92- cmd = ["/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe" , "-Command" , ps_cmd ]
93- else :
94- cmd = ["powershell.exe" , "-Command" , ps_cmd ]
95- else :
96- shutil .rmtree (tmp_dir )
97- return None
98- try :
99- result = subprocess .run (cmd , capture_output = True , timeout = 60 ) # nosec B603 # nosemgrep: python.lang.security.audit.dangerous-subprocess-use-audit
100- if result .returncode == 0 :
101- return tmp_pptx
102- except (subprocess .TimeoutExpired , FileNotFoundError ):
103- pass
104- shutil .rmtree (tmp_dir )
105- return None
16+ """Re-save PPTX with autofit applied via detected presentation backend."""
17+ from sdpm .preview import refresh_autofit_for_convert
18+ return refresh_autofit_for_convert (pptx_path )
10619
10720def pptx_to_json (pptx_path : Path , output_dir : Path = None , no_autofit : bool = False , use_layout_names : bool = True , minimal : bool = False ):
10821 """Convert PPTX to JSON. Output is a project folder with slides.json + images/."""
@@ -111,7 +24,7 @@ def pptx_to_json(pptx_path: Path, output_dir: Path = None, no_autofit: bool = Fa
11124 if not no_autofit :
11225 tmp_pptx = _refresh_autofit_pptx (pptx_path )
11326 if tmp_pptx :
114- print ("Autofit refreshed via PowerPoint " )
27+ print ("Autofit refreshed via presentation backend " )
11528
11629 actual_path = tmp_pptx or pptx_path
11730 prs = Presentation (str (actual_path ))
0 commit comments