-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_python_pptx.py
More file actions
62 lines (45 loc) · 1.79 KB
/
validate_python_pptx.py
File metadata and controls
62 lines (45 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
"""Generate a presentation with python-pptx and validate it.
Requirements:
pip install openxml-audit python-pptx
"""
from pathlib import Path
from tempfile import TemporaryDirectory
from pptx import Presentation
from pptx.util import Inches, Pt
from openxml_audit import FileFormat, OpenXmlValidator
def generate_presentation(path: Path) -> None:
"""Create a sample presentation with python-pptx."""
prs = Presentation()
# Title slide
slide = prs.slides.add_slide(prs.slide_layouts[0])
slide.shapes.title.text = "Quarterly Report"
slide.placeholders[1].text = "Generated with python-pptx"
# Content slide
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "Key Metrics"
body = slide.placeholders[1]
body.text = "Revenue: $1.2M"
body.text_frame.paragraphs[0].font.size = Pt(18)
# Slide with a shape
slide = prs.slides.add_slide(prs.slide_layouts[5])
slide.shapes.add_textbox(Inches(1), Inches(1), Inches(4), Inches(2))
prs.save(str(path))
def main() -> None:
with TemporaryDirectory() as tmp:
path = Path(tmp) / "report.pptx"
print("Generating presentation...")
generate_presentation(path)
print("Validating...")
validator = OpenXmlValidator(file_format=FileFormat.OFFICE_2019)
result = validator.validate(path)
if result.is_valid:
print(f"Valid! ({result.warning_count} warnings)")
else:
print(f"Invalid: {result.error_count} errors, {result.warning_count} warnings")
for error in result.errors:
print(f" [{error.severity.value}] {error.description}")
if error.part_uri:
print(f" Part: {error.part_uri}")
if __name__ == "__main__":
main()