@tiens.nguyen/gu-cli 1.0.902 → 1.0.904
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.
- package/NOTICE +18 -0
- package/package.json +3 -2
- package/skills/core/deep-research/SKILL.md +51 -0
- package/skills/core/docx/LICENSE.txt +202 -0
- package/skills/core/docx/SKILL.md +97 -0
- package/skills/core/docx/scripts/__init__.py +1 -0
- package/skills/core/docx/scripts/accept_changes.py +135 -0
- package/skills/core/docx/scripts/comment.py +368 -0
- package/skills/core/docx/scripts/merge_runs.py +310 -0
- package/skills/core/docx/scripts/office/helpers/__init__.py +121 -0
- package/skills/core/docx/scripts/office/helpers/pptx_chart.py +283 -0
- package/skills/core/docx/scripts/office/helpers/pptx_slide.py +100 -0
- package/skills/core/docx/scripts/office/helpers/pptx_theme.py +114 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
- package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
- package/skills/core/docx/scripts/office/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
- package/skills/core/docx/scripts/office/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
- package/skills/core/docx/scripts/office/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
- package/skills/core/docx/scripts/office/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
- package/skills/core/docx/scripts/office/schemas/mce/mc.xsd +75 -0
- package/skills/core/docx/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
- package/skills/core/docx/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
- package/skills/core/docx/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
- package/skills/core/docx/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
- package/skills/core/docx/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
- package/skills/core/docx/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
- package/skills/core/docx/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
- package/skills/core/docx/scripts/office/soffice.py +192 -0
- package/skills/core/docx/scripts/office/validate.py +210 -0
- package/skills/core/docx/scripts/office/validators/__init__.py +15 -0
- package/skills/core/docx/scripts/office/validators/base.py +874 -0
- package/skills/core/docx/scripts/office/validators/docx.py +519 -0
- package/skills/core/docx/scripts/office/validators/pptx.py +549 -0
- package/skills/core/docx/scripts/office/validators/redlining.py +461 -0
- package/skills/core/docx/scripts/templates/comments.xml +3 -0
- package/skills/core/docx/scripts/templates/commentsExtended.xml +3 -0
- package/skills/core/docx/scripts/templates/commentsExtensible.xml +3 -0
- package/skills/core/docx/scripts/templates/commentsIds.xml +3 -0
- package/skills/core/docx/scripts/templates/people.xml +3 -0
- package/skills/core/pdf/FORMS.md +294 -0
- package/skills/core/pdf/LICENSE.txt +202 -0
- package/skills/core/pdf/REFERENCE.md +613 -0
- package/skills/core/pdf/SKILL.md +314 -0
- package/skills/core/pdf/scripts/check_bounding_boxes.py +65 -0
- package/skills/core/pdf/scripts/check_fillable_fields.py +11 -0
- package/skills/core/pdf/scripts/convert_pdf_to_images.py +33 -0
- package/skills/core/pdf/scripts/create_validation_image.py +37 -0
- package/skills/core/pdf/scripts/extract_form_field_info.py +122 -0
- package/skills/core/pdf/scripts/extract_form_structure.py +115 -0
- package/skills/core/pdf/scripts/fill_fillable_fields.py +98 -0
- package/skills/core/pdf/scripts/fill_pdf_form_with_annotations.py +107 -0
- package/skills/core/pptx/LICENSE.txt +202 -0
- package/skills/core/pptx/SKILL.md +275 -0
- package/skills/core/pptx/scripts/__init__.py +0 -0
- package/skills/core/pptx/scripts/add_slide.py +463 -0
- package/skills/core/pptx/scripts/apply_theme.js +107 -0
- package/skills/core/pptx/scripts/clean.py +309 -0
- package/skills/core/pptx/scripts/office/helpers/__init__.py +121 -0
- package/skills/core/pptx/scripts/office/helpers/pptx_chart.py +283 -0
- package/skills/core/pptx/scripts/office/helpers/pptx_slide.py +100 -0
- package/skills/core/pptx/scripts/office/helpers/pptx_theme.py +114 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
- package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
- package/skills/core/pptx/scripts/office/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
- package/skills/core/pptx/scripts/office/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
- package/skills/core/pptx/scripts/office/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
- package/skills/core/pptx/scripts/office/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
- package/skills/core/pptx/scripts/office/schemas/mce/mc.xsd +75 -0
- package/skills/core/pptx/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
- package/skills/core/pptx/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
- package/skills/core/pptx/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
- package/skills/core/pptx/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
- package/skills/core/pptx/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
- package/skills/core/pptx/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
- package/skills/core/pptx/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
- package/skills/core/pptx/scripts/office/soffice.py +192 -0
- package/skills/core/pptx/scripts/office/validate.py +210 -0
- package/skills/core/pptx/scripts/office/validators/__init__.py +15 -0
- package/skills/core/pptx/scripts/office/validators/base.py +874 -0
- package/skills/core/pptx/scripts/office/validators/docx.py +519 -0
- package/skills/core/pptx/scripts/office/validators/pptx.py +549 -0
- package/skills/core/pptx/scripts/office/validators/redlining.py +461 -0
- package/skills/core/pptx/scripts/slide_text.py +132 -0
- package/skills/core/pptx/scripts/thumbnail.py +311 -0
- package/skills/core/xlsx/LICENSE.txt +202 -0
- package/skills/core/xlsx/SKILL.md +99 -0
- package/skills/core/xlsx/scripts/office/helpers/__init__.py +121 -0
- package/skills/core/xlsx/scripts/office/helpers/pptx_chart.py +283 -0
- package/skills/core/xlsx/scripts/office/helpers/pptx_slide.py +100 -0
- package/skills/core/xlsx/scripts/office/helpers/pptx_theme.py +114 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
- package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
- package/skills/core/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
- package/skills/core/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
- package/skills/core/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
- package/skills/core/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
- package/skills/core/xlsx/scripts/office/schemas/mce/mc.xsd +75 -0
- package/skills/core/xlsx/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
- package/skills/core/xlsx/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
- package/skills/core/xlsx/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
- package/skills/core/xlsx/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
- package/skills/core/xlsx/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
- package/skills/core/xlsx/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
- package/skills/core/xlsx/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
- package/skills/core/xlsx/scripts/office/soffice.py +192 -0
- package/skills/core/xlsx/scripts/office/validate.py +210 -0
- package/skills/core/xlsx/scripts/office/validators/__init__.py +15 -0
- package/skills/core/xlsx/scripts/office/validators/base.py +874 -0
- package/skills/core/xlsx/scripts/office/validators/docx.py +519 -0
- package/skills/core/xlsx/scripts/office/validators/pptx.py +549 -0
- package/skills/core/xlsx/scripts/office/validators/redlining.py +461 -0
- package/skills/core/xlsx/scripts/recalc.py +456 -0
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Validator for tracked changes in Word documents.
|
|
3
|
+
|
|
4
|
+
Detects untracked edits in a Word document: text that differs from the
|
|
5
|
+
original without a <w:ins>/<w:del> wrapper recording it. The tracked changes
|
|
6
|
+
that are new relative to the original are undone, and the result is compared
|
|
7
|
+
against the original; whatever text still differs was edited without being
|
|
8
|
+
tracked.
|
|
9
|
+
|
|
10
|
+
The body (word/document.xml) is compared, and so is each header, footer,
|
|
11
|
+
footnotes and endnotes part, against the original's part of the same name.
|
|
12
|
+
Other parts, comments among them, are not checked. A page-number field
|
|
13
|
+
whose cached result is a plain number counts as its name in braces
|
|
14
|
+
("{PAGE}"), not as that number, which any application that repaginates the
|
|
15
|
+
document rewrites.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import re
|
|
19
|
+
import subprocess
|
|
20
|
+
import tempfile
|
|
21
|
+
import zipfile
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
|
|
24
|
+
import defusedxml.ElementTree as ET
|
|
25
|
+
from defusedxml.common import DefusedXmlException
|
|
26
|
+
|
|
27
|
+
from helpers import rendered_text, safe_extract
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
XML_ERRORS = (ET.ParseError, DefusedXmlException, LookupError, ValueError)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class RedliningValidator:
|
|
34
|
+
|
|
35
|
+
def __init__(self, unpacked_dir, original_docx, verbose=False):
|
|
36
|
+
self.unpacked_dir = Path(unpacked_dir)
|
|
37
|
+
self.original_docx = Path(original_docx)
|
|
38
|
+
self.verbose = verbose
|
|
39
|
+
self.namespaces = {
|
|
40
|
+
"w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
def repair(self) -> int:
|
|
44
|
+
return 0
|
|
45
|
+
|
|
46
|
+
def validate(self):
|
|
47
|
+
modified_file = self.unpacked_dir / "word" / "document.xml"
|
|
48
|
+
if not modified_file.exists():
|
|
49
|
+
print(f"FAILED - Modified document.xml not found at {modified_file}")
|
|
50
|
+
return False
|
|
51
|
+
|
|
52
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
|
53
|
+
temp_path = Path(temp_dir)
|
|
54
|
+
|
|
55
|
+
try:
|
|
56
|
+
with zipfile.ZipFile(self.original_docx, "r") as zip_ref:
|
|
57
|
+
safe_extract(zip_ref, temp_path)
|
|
58
|
+
except Exception as e:
|
|
59
|
+
print(f"FAILED - Error unpacking original docx: {e}")
|
|
60
|
+
return False
|
|
61
|
+
|
|
62
|
+
original_file = temp_path / "word" / "document.xml"
|
|
63
|
+
if not original_file.exists():
|
|
64
|
+
print(
|
|
65
|
+
f"FAILED - Original document.xml not found in {self.original_docx}"
|
|
66
|
+
)
|
|
67
|
+
return False
|
|
68
|
+
|
|
69
|
+
sides = (("original", temp_path), ("modified", self.unpacked_dir))
|
|
70
|
+
change_count, failures, differences = 0, [], []
|
|
71
|
+
for part in self._parts_to_compare(temp_path, self.unpacked_dir):
|
|
72
|
+
roots = []
|
|
73
|
+
for side, unpacked in sides:
|
|
74
|
+
try:
|
|
75
|
+
roots.append(self._parse_part(unpacked / part))
|
|
76
|
+
except XML_ERRORS as e:
|
|
77
|
+
failures.append(
|
|
78
|
+
f"FAILED - Error parsing {part} of the {side} document: {e}"
|
|
79
|
+
)
|
|
80
|
+
if len(roots) < len(sides):
|
|
81
|
+
continue
|
|
82
|
+
new_changes, original_text, modified_text = self._compare_part(*roots)
|
|
83
|
+
change_count += len(new_changes)
|
|
84
|
+
if modified_text != original_text:
|
|
85
|
+
differences.append((part, original_text, modified_text))
|
|
86
|
+
|
|
87
|
+
if differences:
|
|
88
|
+
failures.append(self._generate_detailed_diff(differences))
|
|
89
|
+
if failures:
|
|
90
|
+
print("\n".join(failures))
|
|
91
|
+
return False
|
|
92
|
+
|
|
93
|
+
if self.verbose:
|
|
94
|
+
print(
|
|
95
|
+
f"PASSED - All {change_count} change(s) against the original "
|
|
96
|
+
"are properly tracked"
|
|
97
|
+
)
|
|
98
|
+
return True
|
|
99
|
+
|
|
100
|
+
OTHER_PARTS = (
|
|
101
|
+
"word/header*.xml",
|
|
102
|
+
"word/footer*.xml",
|
|
103
|
+
"word/footnotes.xml",
|
|
104
|
+
"word/endnotes.xml",
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
def _parts_to_compare(self, original_dir, modified_dir):
|
|
108
|
+
others = set()
|
|
109
|
+
for unpacked in (Path(original_dir), Path(modified_dir)):
|
|
110
|
+
for pattern in self.OTHER_PARTS:
|
|
111
|
+
others.update(
|
|
112
|
+
path.relative_to(unpacked).as_posix()
|
|
113
|
+
for path in unpacked.glob(pattern)
|
|
114
|
+
)
|
|
115
|
+
return ["word/document.xml", *sorted(others)]
|
|
116
|
+
|
|
117
|
+
def _compare_part(self, original_root, modified_root):
|
|
118
|
+
for root in (original_root, modified_root):
|
|
119
|
+
self._flatten_text_elements(root)
|
|
120
|
+
new_changes = self._new_tracked_changes(original_root, modified_root)
|
|
121
|
+
self._remove_tracked_changes(modified_root, new_changes)
|
|
122
|
+
return (
|
|
123
|
+
new_changes,
|
|
124
|
+
self._extract_text_content(original_root),
|
|
125
|
+
self._extract_text_content(modified_root),
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
def _parse_part(self, path):
|
|
129
|
+
if not path.is_file():
|
|
130
|
+
return ET.fromstring("<absent/>")
|
|
131
|
+
return ET.parse(path).getroot()
|
|
132
|
+
|
|
133
|
+
def _flatten_text_elements(self, root):
|
|
134
|
+
for elem in self._text_elements(root):
|
|
135
|
+
if len(elem):
|
|
136
|
+
elem.text = "".join(elem.itertext())
|
|
137
|
+
for child in list(elem):
|
|
138
|
+
elem.remove(child)
|
|
139
|
+
|
|
140
|
+
def _tracked_change_elements(self, root):
|
|
141
|
+
w = self.namespaces["w"]
|
|
142
|
+
tags = (f"{{{w}}}ins", f"{{{w}}}del", f"{{{w}}}moveFrom", f"{{{w}}}moveTo")
|
|
143
|
+
return [elem for elem in root.iter() if elem.tag in tags]
|
|
144
|
+
|
|
145
|
+
def _rendered_text(self, elem):
|
|
146
|
+
preserve = elem.get("{http://www.w3.org/XML/1998/namespace}space") == "preserve"
|
|
147
|
+
return rendered_text(elem.text or "", preserve)
|
|
148
|
+
|
|
149
|
+
def _text_elements(self, elem):
|
|
150
|
+
w = self.namespaces["w"]
|
|
151
|
+
return [
|
|
152
|
+
node
|
|
153
|
+
for node in elem.iter()
|
|
154
|
+
if node.tag in (f"{{{w}}}t", f"{{{w}}}delText")
|
|
155
|
+
]
|
|
156
|
+
|
|
157
|
+
def _tracked_change_key(self, elem):
|
|
158
|
+
w = self.namespaces["w"]
|
|
159
|
+
text = "".join(self._rendered_text(node) for node in self._text_elements(elem))
|
|
160
|
+
return (elem.tag, elem.get(f"{{{w}}}author"), elem.get(f"{{{w}}}date"), text)
|
|
161
|
+
|
|
162
|
+
def _new_tracked_changes(self, original_root, modified_root):
|
|
163
|
+
original = self._tracked_change_elements(original_root)
|
|
164
|
+
modified = self._tracked_change_elements(modified_root)
|
|
165
|
+
|
|
166
|
+
pool = {}
|
|
167
|
+
for elem in original:
|
|
168
|
+
pool.setdefault(self._tracked_change_key(elem), []).append(elem)
|
|
169
|
+
|
|
170
|
+
matched, leftover = set(), []
|
|
171
|
+
for elem in modified:
|
|
172
|
+
bucket = pool.get(self._tracked_change_key(elem))
|
|
173
|
+
if bucket:
|
|
174
|
+
matched.add(bucket.pop())
|
|
175
|
+
else:
|
|
176
|
+
leftover.append(elem)
|
|
177
|
+
|
|
178
|
+
def group(elem):
|
|
179
|
+
return self._tracked_change_key(elem)[:3]
|
|
180
|
+
|
|
181
|
+
def text_of(elems):
|
|
182
|
+
return "".join(self._tracked_change_key(e)[3] for e in elems)
|
|
183
|
+
|
|
184
|
+
unmatched_original = {}
|
|
185
|
+
for elem in original:
|
|
186
|
+
if elem not in matched:
|
|
187
|
+
unmatched_original.setdefault(group(elem), []).append(elem)
|
|
188
|
+
|
|
189
|
+
by_group = {}
|
|
190
|
+
for elem in leftover:
|
|
191
|
+
by_group.setdefault(group(elem), []).append(elem)
|
|
192
|
+
|
|
193
|
+
new = set()
|
|
194
|
+
for key, elems in by_group.items():
|
|
195
|
+
rebuilt = text_of(elems)
|
|
196
|
+
if rebuilt and rebuilt == text_of(unmatched_original.get(key, [])):
|
|
197
|
+
continue
|
|
198
|
+
new.update(elems)
|
|
199
|
+
return new
|
|
200
|
+
|
|
201
|
+
def _generate_detailed_diff(self, differences):
|
|
202
|
+
error_parts = [
|
|
203
|
+
"FAILED - Document text doesn't match after removing the tracked changes",
|
|
204
|
+
"",
|
|
205
|
+
"Likely causes:",
|
|
206
|
+
" 1. Modified text inside another author's <w:ins> or <w:del> tags",
|
|
207
|
+
" 2. Made edits without proper tracked changes",
|
|
208
|
+
" 3. Didn't nest <w:del> inside <w:ins> when deleting another's insertion",
|
|
209
|
+
" 4. Rewrote another author's <w:ins>/<w:del> and changed its text on",
|
|
210
|
+
" the way. A tracked change from the original is recognised by its",
|
|
211
|
+
" author, date and text; anything that doesn't reproduce one exactly",
|
|
212
|
+
" reads as new, and the text it carried is reported missing.",
|
|
213
|
+
"",
|
|
214
|
+
"For pre-redlined documents, use correct patterns:",
|
|
215
|
+
" - To reject another's INSERTION: Nest <w:del> inside their <w:ins>",
|
|
216
|
+
" - To reject PART of one: nest <w:del> around only the runs you reject.",
|
|
217
|
+
" Their <w:ins> may be split around it, so long as the pieces keep",
|
|
218
|
+
" their author and date and still spell out the same text.",
|
|
219
|
+
" - To restore another's DELETION: Add new <w:ins> AFTER their <w:del>",
|
|
220
|
+
"",
|
|
221
|
+
]
|
|
222
|
+
|
|
223
|
+
explain_page_fields = False
|
|
224
|
+
for part, original_text, modified_text in differences:
|
|
225
|
+
body = part == "word/document.xml"
|
|
226
|
+
heading = "Differences:" if body else f"Differences in {part}:"
|
|
227
|
+
if error_parts[-1]:
|
|
228
|
+
error_parts.append("")
|
|
229
|
+
git_diff = self._get_git_word_diff(original_text, modified_text)
|
|
230
|
+
if git_diff:
|
|
231
|
+
error_parts.extend([heading, "=" * len(heading), git_diff])
|
|
232
|
+
if re.search(r"\{(%s)\}" % "|".join(self.PAGE_FIELDS), git_diff):
|
|
233
|
+
explain_page_fields = True
|
|
234
|
+
else:
|
|
235
|
+
error_parts.extend(
|
|
236
|
+
[heading, "Unable to generate word diff (git not available)"]
|
|
237
|
+
)
|
|
238
|
+
if explain_page_fields:
|
|
239
|
+
error_parts.extend(["", self.PAGE_FIELD_NOTE])
|
|
240
|
+
|
|
241
|
+
return "\n".join(error_parts)
|
|
242
|
+
|
|
243
|
+
def _get_git_word_diff(self, original_text, modified_text):
|
|
244
|
+
try:
|
|
245
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
|
246
|
+
temp_path = Path(temp_dir)
|
|
247
|
+
|
|
248
|
+
original_file = temp_path / "original.txt"
|
|
249
|
+
modified_file = temp_path / "modified.txt"
|
|
250
|
+
|
|
251
|
+
original_file.write_text(original_text, encoding="utf-8")
|
|
252
|
+
modified_file.write_text(modified_text, encoding="utf-8")
|
|
253
|
+
|
|
254
|
+
result = subprocess.run(
|
|
255
|
+
[
|
|
256
|
+
"git",
|
|
257
|
+
"diff",
|
|
258
|
+
"--word-diff=plain",
|
|
259
|
+
"--word-diff-regex=.",
|
|
260
|
+
"-U0",
|
|
261
|
+
"--no-index",
|
|
262
|
+
str(original_file),
|
|
263
|
+
str(modified_file),
|
|
264
|
+
],
|
|
265
|
+
capture_output=True,
|
|
266
|
+
encoding="utf-8",
|
|
267
|
+
errors="replace",
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
if result.stdout.strip():
|
|
271
|
+
lines = result.stdout.split("\n")
|
|
272
|
+
content_lines = []
|
|
273
|
+
in_content = False
|
|
274
|
+
for line in lines:
|
|
275
|
+
if line.startswith("@@"):
|
|
276
|
+
in_content = True
|
|
277
|
+
continue
|
|
278
|
+
if in_content and line.strip():
|
|
279
|
+
content_lines.append(line)
|
|
280
|
+
|
|
281
|
+
if content_lines:
|
|
282
|
+
return "\n".join(content_lines)
|
|
283
|
+
|
|
284
|
+
result = subprocess.run(
|
|
285
|
+
[
|
|
286
|
+
"git",
|
|
287
|
+
"diff",
|
|
288
|
+
"--word-diff=plain",
|
|
289
|
+
"-U0",
|
|
290
|
+
"--no-index",
|
|
291
|
+
str(original_file),
|
|
292
|
+
str(modified_file),
|
|
293
|
+
],
|
|
294
|
+
capture_output=True,
|
|
295
|
+
encoding="utf-8",
|
|
296
|
+
errors="replace",
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
if result.stdout.strip():
|
|
300
|
+
lines = result.stdout.split("\n")
|
|
301
|
+
content_lines = []
|
|
302
|
+
in_content = False
|
|
303
|
+
for line in lines:
|
|
304
|
+
if line.startswith("@@"):
|
|
305
|
+
in_content = True
|
|
306
|
+
continue
|
|
307
|
+
if in_content and line.strip():
|
|
308
|
+
content_lines.append(line)
|
|
309
|
+
return "\n".join(content_lines)
|
|
310
|
+
|
|
311
|
+
except (subprocess.CalledProcessError, FileNotFoundError, Exception):
|
|
312
|
+
pass
|
|
313
|
+
|
|
314
|
+
return None
|
|
315
|
+
|
|
316
|
+
def _remove_tracked_changes(self, root, targets):
|
|
317
|
+
w = self.namespaces["w"]
|
|
318
|
+
ins_tag = f"{{{w}}}ins"
|
|
319
|
+
del_tag = f"{{{w}}}del"
|
|
320
|
+
move_from_tag = f"{{{w}}}moveFrom"
|
|
321
|
+
move_to_tag = f"{{{w}}}moveTo"
|
|
322
|
+
|
|
323
|
+
self._merge_split_paragraphs(root, targets)
|
|
324
|
+
|
|
325
|
+
for parent in root.iter():
|
|
326
|
+
to_remove = []
|
|
327
|
+
for child in parent:
|
|
328
|
+
if child.tag in (ins_tag, move_to_tag) and child in targets:
|
|
329
|
+
to_remove.append(child)
|
|
330
|
+
for elem in to_remove:
|
|
331
|
+
parent.remove(elem)
|
|
332
|
+
|
|
333
|
+
renamed = {
|
|
334
|
+
f"{{{w}}}delText": f"{{{w}}}t",
|
|
335
|
+
f"{{{w}}}delInstrText": f"{{{w}}}instrText",
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
for parent in root.iter():
|
|
339
|
+
to_process = []
|
|
340
|
+
for child in parent:
|
|
341
|
+
if child.tag in (del_tag, move_from_tag) and child in targets:
|
|
342
|
+
to_process.append((child, list(parent).index(child)))
|
|
343
|
+
|
|
344
|
+
for del_elem, del_index in reversed(to_process):
|
|
345
|
+
for elem in del_elem.iter():
|
|
346
|
+
elem.tag = renamed.get(elem.tag, elem.tag)
|
|
347
|
+
|
|
348
|
+
for child in reversed(list(del_elem)):
|
|
349
|
+
parent.insert(del_index, child)
|
|
350
|
+
parent.remove(del_elem)
|
|
351
|
+
|
|
352
|
+
range_tags = (
|
|
353
|
+
f"{{{w}}}moveFromRangeStart",
|
|
354
|
+
f"{{{w}}}moveFromRangeEnd",
|
|
355
|
+
f"{{{w}}}moveToRangeStart",
|
|
356
|
+
f"{{{w}}}moveToRangeEnd",
|
|
357
|
+
)
|
|
358
|
+
for parent in root.iter():
|
|
359
|
+
to_remove = [
|
|
360
|
+
c
|
|
361
|
+
for c in parent
|
|
362
|
+
if c.tag in range_tags and len(c) == 0 and not (c.text or "").strip()
|
|
363
|
+
]
|
|
364
|
+
for elem in to_remove:
|
|
365
|
+
parent.remove(elem)
|
|
366
|
+
|
|
367
|
+
def _merge_split_paragraphs(self, root, targets):
|
|
368
|
+
w = self.namespaces["w"]
|
|
369
|
+
p_tag = f"{{{w}}}p"
|
|
370
|
+
ppr_tag = f"{{{w}}}pPr"
|
|
371
|
+
marker_path = f"{ppr_tag}/{{{w}}}rPr/{{{w}}}ins"
|
|
372
|
+
|
|
373
|
+
for parent in list(root.iter()):
|
|
374
|
+
for para in reversed([child for child in parent if child.tag == p_tag]):
|
|
375
|
+
if not any(m in targets for m in para.findall(marker_path)):
|
|
376
|
+
continue
|
|
377
|
+
siblings = list(parent)
|
|
378
|
+
index = siblings.index(para)
|
|
379
|
+
following = siblings[index + 1] if index + 1 < len(siblings) else None
|
|
380
|
+
if following is None or following.tag != p_tag:
|
|
381
|
+
continue
|
|
382
|
+
if any(self._text_elements(c) for c in para if c.tag == ppr_tag):
|
|
383
|
+
continue
|
|
384
|
+
insert_at = 1 if len(following) and following[0].tag == ppr_tag else 0
|
|
385
|
+
for child in reversed([c for c in para if c.tag != ppr_tag]):
|
|
386
|
+
following.insert(insert_at, child)
|
|
387
|
+
parent.remove(para)
|
|
388
|
+
|
|
389
|
+
def _extract_text_content(self, root):
|
|
390
|
+
p_tag = f"{{{self.namespaces['w']}}}p"
|
|
391
|
+
|
|
392
|
+
paragraphs = []
|
|
393
|
+
for p_elem in root.findall(f".//{p_tag}"):
|
|
394
|
+
paragraph_text = self._paragraph_text(p_elem)
|
|
395
|
+
if paragraph_text:
|
|
396
|
+
paragraphs.append(paragraph_text)
|
|
397
|
+
|
|
398
|
+
return "\n".join(paragraphs)
|
|
399
|
+
|
|
400
|
+
PAGE_FIELDS = {"PAGE", "NUMPAGES", "SECTIONPAGES"}
|
|
401
|
+
PAGE_NUMBER = re.compile(r"-? ?[0-9]{1,6} ?-?|[ivxlcdm]{1,8}|[a-z]{1,2}|", re.I)
|
|
402
|
+
PAGE_FIELD_NOTE = (
|
|
403
|
+
"Note: {PAGE}, {NUMPAGES} or {SECTIONPAGES} above stands for a page-number\n"
|
|
404
|
+
"field. Word recomputes the number it displays, so the number stored in\n"
|
|
405
|
+
"the file is not compared, but adding or removing the field still counts."
|
|
406
|
+
)
|
|
407
|
+
|
|
408
|
+
def _paragraph_text(self, p_elem):
|
|
409
|
+
w = self.namespaces["w"]
|
|
410
|
+
t_tag, instr_tag = f"{{{w}}}t", f"{{{w}}}instrText"
|
|
411
|
+
fld_char, fld_simple = f"{{{w}}}fldChar", f"{{{w}}}fldSimple"
|
|
412
|
+
|
|
413
|
+
def page_field(instruction, field):
|
|
414
|
+
words = instruction.split()
|
|
415
|
+
locked = field.get(f"{{{w}}}fldLock") in ("1", "true", "on")
|
|
416
|
+
if words and words[0].upper() in self.PAGE_FIELDS and not locked:
|
|
417
|
+
return words[0].upper()
|
|
418
|
+
|
|
419
|
+
def shown(name, result):
|
|
420
|
+
return "{%s}" % name if self.PAGE_NUMBER.fullmatch(result) else result
|
|
421
|
+
|
|
422
|
+
parts, fields = [], []
|
|
423
|
+
skipped = set()
|
|
424
|
+
|
|
425
|
+
def sink():
|
|
426
|
+
for *_, result in reversed(fields):
|
|
427
|
+
if result is not None:
|
|
428
|
+
return result
|
|
429
|
+
return parts
|
|
430
|
+
|
|
431
|
+
for elem in p_elem.iter():
|
|
432
|
+
if elem.tag == fld_simple:
|
|
433
|
+
name = page_field(elem.get(f"{{{w}}}instr", ""), elem)
|
|
434
|
+
if name:
|
|
435
|
+
texts = [self._rendered_text(t) for t in elem.iter(t_tag)]
|
|
436
|
+
sink().append(shown(name, "".join(texts).strip()))
|
|
437
|
+
skipped.update(elem.iter(t_tag))
|
|
438
|
+
elif elem.tag == fld_char:
|
|
439
|
+
kind = elem.get(f"{{{w}}}fldCharType")
|
|
440
|
+
if kind == "begin":
|
|
441
|
+
fields.append([elem, "", False, None])
|
|
442
|
+
elif kind == "separate" and fields and not fields[-1][2]:
|
|
443
|
+
fields[-1][2] = True
|
|
444
|
+
if page_field(fields[-1][1], fields[-1][0]):
|
|
445
|
+
fields[-1][3] = []
|
|
446
|
+
elif kind == "end" and fields:
|
|
447
|
+
begin, instruction, _, result = fields.pop()
|
|
448
|
+
name = page_field(instruction, begin)
|
|
449
|
+
if name:
|
|
450
|
+
sink().append(shown(name, "".join(result or []).strip()))
|
|
451
|
+
elif elem.tag == instr_tag and fields and not fields[-1][2]:
|
|
452
|
+
fields[-1][1] += elem.text or ""
|
|
453
|
+
elif elem.tag == t_tag and elem not in skipped:
|
|
454
|
+
sink().append(self._rendered_text(elem))
|
|
455
|
+
for *_, result in fields:
|
|
456
|
+
parts.extend(result or [])
|
|
457
|
+
return "".join(parts)
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
if __name__ == "__main__":
|
|
461
|
+
raise RuntimeError("This module should not be run directly.")
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"""Print each slide of a .pptx, .pptm, .potx or .ppsx as markdown under `<!-- Slide number: N -->`.
|
|
2
|
+
|
|
3
|
+
Title, text, tables, chart data, picture alt text and speaker notes; shapes top to bottom.
|
|
4
|
+
|
|
5
|
+
Usage: python slide_text.py deck.pptx
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
import re
|
|
10
|
+
import sys
|
|
11
|
+
import zipfile
|
|
12
|
+
|
|
13
|
+
from pptx.package import Package
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _position(shape):
|
|
17
|
+
try:
|
|
18
|
+
return (shape.top or 0, shape.left or 0)
|
|
19
|
+
except Exception:
|
|
20
|
+
return (0, 0)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _one_line(text) -> str:
|
|
24
|
+
return " ".join(str(text).split())
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _table(rows) -> list[str]:
|
|
28
|
+
lines = ["| " + " | ".join(_one_line(c).replace("|", "\\|") for c in r) + " |" for r in rows]
|
|
29
|
+
if lines:
|
|
30
|
+
lines.insert(1, "|" + " --- |" * len(rows[0]))
|
|
31
|
+
return lines
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _chart(chart) -> list[str]:
|
|
35
|
+
heading = "### Chart"
|
|
36
|
+
try:
|
|
37
|
+
if chart.has_title and chart.chart_title.has_text_frame:
|
|
38
|
+
heading += ": " + _one_line(chart.chart_title.text_frame.text)
|
|
39
|
+
categories = [c.label for c in chart.plots[0].categories]
|
|
40
|
+
series = [(s.name, list(s.values)) for s in chart.series]
|
|
41
|
+
except Exception as e:
|
|
42
|
+
return [
|
|
43
|
+
heading,
|
|
44
|
+
f"[chart data not readable ({type(e).__name__}): render the slide to see it]",
|
|
45
|
+
]
|
|
46
|
+
rows = [["Category", *(name for name, _ in series)]]
|
|
47
|
+
for i, category in enumerate(categories):
|
|
48
|
+
rows.append([category, *(v[i] if i < len(v) else "" for _, v in series)])
|
|
49
|
+
return [heading, "", *_table(rows)]
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _shape(shape) -> list[str]:
|
|
53
|
+
tag = shape._element.tag
|
|
54
|
+
if tag.endswith("}grpSp"):
|
|
55
|
+
return _shapes(shape.shapes)
|
|
56
|
+
if tag.endswith("}pic"):
|
|
57
|
+
alt = _one_line(shape._element._nvXxPr.cNvPr.get("descr", ""))
|
|
58
|
+
return [f""]
|
|
59
|
+
if getattr(shape, "has_table", False):
|
|
60
|
+
return ["", *_table([[c.text for c in r.cells] for r in shape.table.rows]), ""]
|
|
61
|
+
if getattr(shape, "has_chart", False):
|
|
62
|
+
return ["", *_chart(shape.chart), ""]
|
|
63
|
+
if shape.has_text_frame and shape.text_frame.text.strip():
|
|
64
|
+
text = shape.text_frame.text.replace("\v", "\n")
|
|
65
|
+
is_title = shape.is_placeholder and shape.placeholder_format.idx == 0
|
|
66
|
+
return ["# " + text.lstrip() if is_title else text]
|
|
67
|
+
return []
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _members(shapes) -> list:
|
|
71
|
+
try:
|
|
72
|
+
return list(shapes)
|
|
73
|
+
except Exception:
|
|
74
|
+
pass
|
|
75
|
+
members = []
|
|
76
|
+
for i in range(len(shapes)):
|
|
77
|
+
try:
|
|
78
|
+
members.append(shapes[i])
|
|
79
|
+
except Exception as e:
|
|
80
|
+
members.append(e)
|
|
81
|
+
return members
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _shapes(shapes) -> list[str]:
|
|
85
|
+
lines = []
|
|
86
|
+
for shape in sorted(_members(shapes), key=_position):
|
|
87
|
+
try:
|
|
88
|
+
if isinstance(shape, Exception):
|
|
89
|
+
raise shape
|
|
90
|
+
lines += _shape(shape)
|
|
91
|
+
except Exception as e:
|
|
92
|
+
lines.append(f"[shape not readable ({type(e).__name__}): render the slide to see it]")
|
|
93
|
+
return lines
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def slide_text(slide, number: int) -> str:
|
|
97
|
+
lines = [f"<!-- Slide number: {number} -->", *_shapes(slide.shapes)]
|
|
98
|
+
try:
|
|
99
|
+
notes = slide.notes_slide.notes_text_frame if slide.has_notes_slide else None
|
|
100
|
+
if notes is not None and notes.text.strip():
|
|
101
|
+
lines += ["", "### Notes:", notes.text.replace("\v", "\n")]
|
|
102
|
+
except Exception as e:
|
|
103
|
+
lines += ["", f"[notes not readable ({type(e).__name__})]"]
|
|
104
|
+
return re.sub(r"\n{3,}", "\n\n", "\n".join(lines).strip())
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def main() -> None:
|
|
108
|
+
if len(sys.argv) != 2:
|
|
109
|
+
sys.exit("Usage: python slide_text.py deck.pptx")
|
|
110
|
+
path = sys.argv[1]
|
|
111
|
+
if not os.path.isfile(path):
|
|
112
|
+
sys.exit(f"Error: {path} {'is not a file' if os.path.exists(path) else 'does not exist'}")
|
|
113
|
+
if not zipfile.is_zipfile(path):
|
|
114
|
+
sys.exit(
|
|
115
|
+
f"Error: {path} is corrupt, password-protected, or an old .ppt (not a zip archive)"
|
|
116
|
+
)
|
|
117
|
+
try:
|
|
118
|
+
deck = getattr(Package.open(path).main_document_part, "presentation", None)
|
|
119
|
+
except Exception as e:
|
|
120
|
+
sys.exit(f"Error: cannot read {path}: {e}")
|
|
121
|
+
if deck is None:
|
|
122
|
+
sys.exit(f"Error: {path} is not a .pptx, .pptm, .potx or .ppsx file")
|
|
123
|
+
sys.stdout.reconfigure(errors="replace")
|
|
124
|
+
try:
|
|
125
|
+
blocks = [slide_text(s, n) for n, s in enumerate(deck.slides, 1)]
|
|
126
|
+
print("\n\n".join(blocks) or "[no slides; layouts and masters are not printed]", flush=True)
|
|
127
|
+
except BrokenPipeError:
|
|
128
|
+
os.dup2(os.open(os.devnull, os.O_WRONLY), sys.stdout.fileno())
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
if __name__ == "__main__":
|
|
132
|
+
main()
|