I’m working on a Python script to generate PowerPoint slides from a JSON file. The script reads slide content from the JSON file, converts MathML to OMML for mathematical equations, and then inserts the content into a PowerPoint template. However, when I try to open the generated output.pptx file, PowerPoint displays an error:
PowerPoint found a problem with content in output.pptx
PowerPoint can attempt to repair the presentation. If you trust the source of this presentation, click Repair.
When I click “Repair,” PowerPoint attempts to fix the presentation, but the formatting is often broken, and some content is lost.
Here is the relevant part of my code:
def convert_mathml_to_omml(mathml):
try:
tree = etree.fromstring(mathml)
xslt = etree.parse(r'D:MOMLPMOMLPstaticMML2OMML.XSL')
transform = etree.XSLT(xslt)
new_dom = transform(tree)
return new_dom
except etree.XMLSyntaxError as xml_err:
print(f"XML Syntax Error: {xml_err}")
except etree.XSLTParseError as xslt_err:
print(f"XSLT Parse Error: {xslt_err}")
except etree.XSLTApplyError as apply_err:
print(f"XSLT Apply Error: {apply_err}")
except Exception as err:
print(f"General Error: {err}")
return None
def add_slide(prs, title, subtitle=None, content=None):
try:
slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(slide_layout)
title_placeholder = slide.shapes.title
content_placeholder = slide.placeholders[1]
title_placeholder.left = Inches(0.5)
title_placeholder.top = Inches(0.5)
title_placeholder.width = Inches(9)
title_placeholder.height = Inches(1.5)
title_placeholder.text = title
if subtitle:
title_placeholder.text += f"n{subtitle}"
for paragraph in title_placeholder.text_frame.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(15)
run.font.bold = True
content_text = []
mathml_items = []
if content:
for item in content:
if isinstance(item, str):
content_text.append(item)
elif isinstance(item, dict) and 'mathml' in item:
mathml_items.append(item['mathml'])
content_text.append("")
content_placeholder.left = Inches(1.5)
content_placeholder.top = Inches(2.0)
content_placeholder.width = Inches(9)
content_placeholder.height = Inches(5.5)
if content_text:
combined_text = 'n'.join(content_text)
content_placeholder.text = combined_text
if mathml_items:
nsmap = {
'a14': "http://schemas.microsoft.com/office/drawing/2010/main",
'm': "http://schemas.openxmlformats.org/officeDocument/2006/math"
}
wrapper = etree.Element(
'{http://schemas.microsoft.com/office/drawing/2010/main}m',
nsmap=nsmap
)
omml_para = etree.SubElement(
wrapper,
'{http://schemas.openxmlformats.org/officeDocument/2006/math}oMathPara'
)
for mathml in mathml_items:
omml = convert_mathml_to_omml(mathml)
if omml is not None:
omml_para.append(omml.getroot())
p = content_placeholder.text_frame.add_paragraph()
p._element.append(wrapper)
p = content_placeholder.text_frame.add_paragraph()
p = content_placeholder.text_frame.add_paragraph()
for paragraph in content_placeholder.text_frame.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(15)
run.font.name = 'Calibri'
run.font.bold = False
except Exception as err:
print(f"Error adding slide: {err}")
def main(json_file, template_pptx, output_pptx):
try:
prs = Presentation(template_pptx)
with open(json_file, 'r') as file:
slides = json.load(file)
for i, slide in enumerate(slides):
title = slide.get('title', '')
subtitle = slide.get('subtitle', None)
content = slide.get('content', [])
add_slide(prs, title, subtitle, content)
if i == 0:
title_slide = prs.slides[0]
title_shape = title_slide.shapes.title
title_shape.text = title
prs.save(output_pptx)
except Exception as err:
print(err)
Details:
- PowerPoint Template: I’m using a custom PowerPoint template (template.pptx).
- MathML to OMML Conversion: I’m converting MathML to OMML using an XSLT file (MML2OMML.XSL).
- JSON Input: The script reads content from a JSON file and generates slides based on the data.
- Error Handling: The script includes error handling, but the generated PPTX still seems to have some corruption issues.
Problem:
Despite the error handling and the content being generated correctly as far as I can tell, PowerPoint reports an issue with the file when attempting to open it. I suspect it might be related to the way I’m embedding the OMML content, but I’m not sure.
- What could be causing PowerPoint to throw this error?
- Is there a better way to embed MathML/OMML in PowerPoint using Python?
- How can I debug or validate the PowerPoint XML to identify the specific issue?
Any help or insights would be greatly appreciated!