python-pptx icon indicating copy to clipboard operation
python-pptx copied to clipboard

feat: save as PowerPoint XML Presentation (single .xml file)

Open me-kell opened this issue 8 months ago • 6 comments

For developing purposes it would be nice to have a save option as XML (aka ppSaveAsXMLPresentation). Then it would not be necessary to unpacking the PPTX-file, reformating it and browsing through the diferent part files. See SaveAs ppSaveAsXMLPresentation

As a workaround I am using following PowerShell script which only works in Windows.

$Application = New-Object -ComObject PowerPoint.Application
$filename = "example.pptx"

$Presentation = $Application.Presentations.Open(
    $filename, 
    [Microsoft.Office.Core.MsoTriState]::msoFalse,
    [Microsoft.Office.Core.MsoTriState]::msoFalse,
    [Microsoft.Office.Core.MsoTriState]::msoFalse
)
$Presentation.SaveAs(
    "${filename}_ppSaveAsXMLPresentation.xml", 
    [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsXMLPresentation
)
$Presentation.close()
$Application.Quit()

[System.Runtime.Interopservices.Marshal]::ReleaseComObject([System.__ComObject]$Application) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

function Format-XML {Param ([string]$xmlFile)
    $xmlDataDocument=New-Object system.xml.xmlDataDocument
    $xmlDataDocument.Load((Resolve-Path $xmlFile))
    $stringWriter=New-Object system.io.stringwriter
    $xmlWriter=New-Object system.xml.xmltextwriter($stringWriter)
    $xmlWriter.Formatting = [System.xml.formatting]::Indented
    $xmlWriter.Indentation = 4
    $xmlWriter.Flush()
    $stringWriter.Flush() 
    $xmlDataDocument.WriteContentTo($xmlWriter)
    $stringWriter.ToString()
} 

Format-XML "${filename}_ppSaveAsXMLPresentation.xml" | 
    Set-Content -Encoding utf8 "${filename}_ppSaveAsXMLPresentation-Formatted.xml"

me-kell avatar Apr 30 '25 14:04 me-kell