diff --git a/docs/user-guide.html b/docs/user-guide.html index 89561f3..d2fdae2 100644 --- a/docs/user-guide.html +++ b/docs/user-guide.html @@ -1313,6 +1313,12 @@
Transition
+run-python
.run-python
. Added 3 exit routine points where user Python code can run.Transition
The default is no
- which prevents slide transition effects being generated.
You can override this value with Dynamic Metadata.
+ +With md2pptx you can specify python “exit routines” - files containing python code.
+These routines run
+Each routine has access to the python-pptx Presentation object - in variable prs
.
Before writing or using exit routines others have supplied see An Important Caution.
+Note: You can run python within an individual slide’s processing. +See Running Inline Python.
+ +onPresentationInitialisation
With suitable code at this exit point you could adjust the presentation’s properties. +For example
+from pptx.util import Inches
+
+prs.slide_height = Inches(10.0)
+
+This would, obviously set the height of each slide to 10 inches.
+ +onPresentationBeforeSave
Again, this exit point could allow you to adjust properties of the presentation. +This is the last opportunity before the presentation is saved.
+ +onPresentationAfterSave
When this code runs the prs
variable is still available.
+You might use this exit point to save an adjusted copy of the presentation.
+For example
prs.save("Mycopy.pptx")
+
+Or you might want to run some analysis on the presentation you’ve just created.
md2pptx can alter some in-effect settings, starting at a particular slide. Straight after the heading code a special form of (HTML) comment like so:
@@ -4738,14 +4778,15 @@The support described in Running Inline Python allows you to run arbitary python code. It would be unwise to embed python code of unknown provenance.
-Use only code you directly write (or, of known provenance, embedded with mdpre‘s =include
capability or as an optional parameter to the invocation).
=include
capability, or as an optional parameter to the invocation).
In general, though, this function is worth exploring - if it enables you turn flat files into presentations that you otherwise couldn’t make that way.
There are two ways you can invoke Python in md2pptx:
+There are three ways you can invoke Python in md2pptx:
Note: You can invoke Python in a file from inline python.
diff --git a/docs/user-guide.log b/docs/user-guide.log index aac4482..d127d2b 100644 --- a/docs/user-guide.log +++ b/docs/user-guide.log @@ -5,8 +5,8 @@ mdpre Markdown Preprocessor v0.6.9 (17 September, 2024) Def mdpre_date = 17 September, 2024 Def mdpre_level = 0.6.9 Def userid = martinpacker -Def time = 17:26 -Def date = 22 February, 2025 +Def time = 16:09 +Def date = 23 February, 2025 Def TOC = Table Of Contents Def md = Markdown Def pp = Powerpoint @@ -192,6 +192,10 @@ CSV Stop ..... ..... ..... ..... Section Navigation Button Colour - `SectionArrowsColour` ..... ..... ..... ..... Make Expandable Sections - `SectionsExpand` ..... ..... ..... Slide Transitions - `Transition` +..... ..... ..... Python Exit Routines +..... ..... ..... ..... After Loading - `onPresentationInitialisation` +..... ..... ..... ..... Before Saving - `onPresentationBeforeSave` +..... ..... ..... ..... After Saving - `onPresentationAfterSave` ..... ..... Dynamic Metadata ..... ..... ..... `hidden` ..... ..... ..... Tables diff --git a/docs/user-guide.md b/docs/user-guide.md index e546525..0138149 100644 --- a/docs/user-guide.md +++ b/docs/user-guide.md @@ -180,6 +180,10 @@ As you can see in the [change log](#change-log), md2pptx is frequently updated - * [Section Navigation Button Colour - `SectionArrowsColour`](#section-navigation-button-colour-sectionarrowscolour) * [Make Expandable Sections - `SectionsExpand`](#make-expandable-sections-sectionsexpand) * [Slide Transitions - `Transition`](#slide-transitions-transition) + * [Python Exit Routines](#python-exit-routines) + * [After Loading - `onPresentationInitialisation`](#after-loading-onpresentationinitialisation) + * [Before Saving - `onPresentationBeforeSave`](#before-saving-onpresentationbeforesave) + * [After Saving - `onPresentationAfterSave`](#after-saving-onpresentationaftersave) * [Dynamic Metadata](#dynamic-metadata) * [`hidden`](#hidden) * [Tables](#tables) @@ -445,7 +449,7 @@ To quote from the python-pptx license statement: |Level|Date|What| |:-|-:|:-| -|5.3.1+|22 February 2025|Added the ability to run Python code from named files in [`run-python`](#running-inline-python).| +|5.4|23 February 2025|Added the ability to run Python code from named files in [`run-python`](#running-inline-python). Added [3 exit routine points](#python-exit-routines) where user Python code can run.| |5.3.1|16 February 2025|Added a double-headed arrow, an oval, double lines, and line widths to [Annotations](#annotations-related-helper-routines). Added optional drop shadows for tables with [`tableShadow`](#controlling-whether-tables-have-drop-shadows-tableshadow).| |5.3|11 February 2025|Added [Annotations](#annotations-related-helper-routines) to [`run-python`](#running-inline-python).| |5.2.2|31 December 2024|[Checklist](#checklist-related-helper-routines) items can be richer, for example using `` elements.| @@ -3237,6 +3241,53 @@ The default is `no` - which prevents slide transition effects being generated. You can override this value with [Dynamic Metadata](#transition-dynamic). + +#### Python Exit Routines + +With md2pptx you can specify python "exit routines" - files containing python code. + +These routines run + +* [When any template presentation has been loaded](#after-loading-onpresentationinitialisation), or even if none has been specified. +* [Just before the presentation is saved](#before-saving-onpresentationbeforesave). +* [Just after the presentation is saved](#after-saving-onpresentationaftersave). + +Each routine has access to the python-pptx [Presentation](https://python-pptx.readthedocs.io/en/latest/api/presentation.html#presentation-objects) object - in variable `prs`. + +Before writing or using exit routines others have supplied see [An Important Caution](#an-important-caution). + +**Note:** You can run python within an individual slide's processing. +See [Running Inline Python](#running-inline-python). + + +##### After Loading - `onPresentationInitialisation` + +With suitable code at this exit point you could adjust the presentation's properties. +For example + + from pptx.util import Inches + + prs.slide_height = Inches(10.0) + +This would, obviously set the height of each slide to 10 inches. + + +##### Before Saving - `onPresentationBeforeSave` + +Again, this exit point could allow you to adjust properties of the presentation. +This is the last opportunity before the presentation is saved. + + +##### After Saving - `onPresentationAfterSave` + +When this code runs the `prs` variable is still available. +You might use this exit point to save an adjusted copy of the presentation. +For example + + prs.save("Mycopy.pptx") + +Or you might want to run some analysis on the presentation you've just created. + ### Dynamic Metadata @@ -3710,17 +3761,18 @@ For more information see [ContentSplit](#split-proportions-contentsplit). ### An Important Caution The support described in [Running Inline Python](#running-inline-python) allows you to run **arbitary python code**. It would be unwise to embed python code of unknown provenance. -Use only code you directly write (or, of known provenance, embedded with [mdpre](https://github.com/MartinPacker/mdpre)'s `=include` capability or as an optional parameter to the invocation). +Use only code you directly write (or, of known provenance, embedded with [mdpre](https://github.com/MartinPacker/mdpre)'s `=include` capability, or as an optional parameter to the invocation). In general, though, this function is worth exploring - if it enables you turn flat files into presentations that you otherwise couldn't make that way. ### How To Invoke Python In md2pptx -There are two ways you can invoke Python in md2pptx: +There are three ways you can invoke Python in md2pptx: * [Coding Inline Python](#coding-inline-python) * [Importing Python From A File](#importing-python-from-a-file) +* [Python Exit Routines](#python-exit-routines) **Note:** You can invoke Python in a file from inline python. diff --git a/docs/user-guide.mdp b/docs/user-guide.mdp index 66eaaab..814d347 100644 --- a/docs/user-guide.mdp +++ b/docs/user-guide.mdp @@ -162,7 +162,7 @@ To quote from the python-pptx license statement: =colalign l r l =csv Level,Date,What -5.3.1+,22 February 2025,Added the ability to run Python code from named files in [`run-python`](#running-inline-python). +5.4,23 February 2025,Added the ability to run Python code from named files in [`run-python`](#running-inline-python). Added [3 exit routine points](#python-exit-routines) where user Python code can run. 5.3.1,16 February 2025,Added a double-headed arrow, an oval, double lines, and line widths to [Annotations](#annotations-related-helper-routines). Added optional drop shadows for tables with [`tableShadow`](#controlling-whether-tables-have-drop-shadows-tableshadow). 5.3,11 February 2025,Added [Annotations](#annotations-related-helper-routines) to [`run-python`](#running-inline-python). 5.2.2,31 December 2024,[Checklist](#checklist-related-helper-routines) items can be richer, for example using `` elements. @@ -3267,6 +3267,53 @@ The default is `no` - which prevents slide transition effects being generated. You can override this value with [Dynamic Metadata](#transition-dynamic). + +#### Python Exit Routines + +With md2pptx you can specify python "exit routines" - files containing python code. + +These routines run + +* [When any template presentation has been loaded](#after-loading-onpresentationinitialisation), or even if none has been specified. +* [Just before the presentation is saved](#before-saving-onpresentationbeforesave). +* [Just after the presentation is saved](#after-saving-onpresentationaftersave). + +Each routine has access to the python-pptx [Presentation](https://python-pptx.readthedocs.io/en/latest/api/presentation.html#presentation-objects) object - in variable `prs`. + +Before writing or using exit routines others have supplied see [An Important Caution](#an-important-caution). + +**Note:** You can run python within an individual slide's processing. +See [Running Inline Python](#running-inline-python). + + +##### After Loading - `onPresentationInitialisation` + +With suitable code at this exit point you could adjust the presentation's properties. +For example + + from pptx.util import Inches + + prs.slide_height = Inches(10.0) + +This would, obviously set the height of each slide to 10 inches. + + +##### Before Saving - `onPresentationBeforeSave` + +Again, this exit point could allow you to adjust properties of the presentation. +This is the last opportunity before the presentation is saved. + + +##### After Saving - `onPresentationAfterSave` + +When this code runs the `prs` variable is still available. +You might use this exit point to save an adjusted copy of the presentation. +For example + + prs.save("Mycopy.pptx") + +Or you might want to run some analysis on the presentation you've just created. + ### Dynamic Metadata @@ -3770,17 +3817,18 @@ For more information see [ContentSplit](#split-proportions-contentsplit). The support described in [Running Inline Python](#running-inline-python) allows you to run **arbitary python code**. \ It would be unwise to embed python code of unknown provenance. -Use only code you directly write (or, of known provenance, embedded with [mdpre](https://github.com/MartinPacker/mdpre)'s `=include` capability or as an optional parameter to the invocation). +Use only code you directly write (or, of known provenance, embedded with [mdpre](https://github.com/MartinPacker/mdpre)'s `=include` capability, or as an optional parameter to the invocation). In general, though, this function is worth exploring - if it enables you turn flat files into presentations that you otherwise couldn't make that way. ### How To Invoke Python In md2pptx -There are two ways you can invoke Python in md2pptx: +There are three ways you can invoke Python in md2pptx: * [Coding Inline Python](#coding-inline-python) * [Importing Python From A File](#importing-python-from-a-file) +* [Python Exit Routines](#python-exit-routines) **Note:** You can invoke Python in a file from inline python. diff --git a/md2pptx b/md2pptx index 4708a97..563e0f2 100755 --- a/md2pptx +++ b/md2pptx @@ -86,8 +86,8 @@ except: have_graphviz = False -md2pptx_level = "5.3.1+" -md2pptx_date = "22 February, 2025" +md2pptx_level = "5.4" +md2pptx_date = "23 February, 2025" namespaceURL = { "mc": "http://schemas.openxmlformats.org/markup-compatibility/2006", @@ -4566,6 +4566,15 @@ globals.processingOptions.setOptionValuesArray( ] ) +# "on" exit files initialisation +globals.processingOptions.setOptionValuesArray( + [ + ["onPresentationInitialisation", ""], + ["onPresentationBeforeSave", ""], + ["onPresentationAfterSave", ""], + ] +) + ###################################################################################### # # # Prime for footnotes # @@ -4665,7 +4674,13 @@ for line in metadata_lines: elif name == "backgroundimage": globals.processingOptions.setOptionValues(name, value) - + elif name in [ + "onpresentationinitialisation", + "onpresentationbeforesave", + "onpresentationaftersave", + ]: + globals.processingOptions.setOptionValues(name, value) + print(value, "<===") elif name in [ "sectiontitlesize", @@ -5125,6 +5140,11 @@ else: for slide in prs.slides: slide.slideInfo = None +# Maybe call an exit as the presentation is initialised +onPresentationInitialisation = globals.processingOptions.getCurrentOption("onPresentationInitialisation") +if onPresentationInitialisation != "": + exec(open(onPresentationInitialisation).read()) + # Following might be used in slide footers prs.lastSectionTitle = "" prs.lastSectionSlide = None @@ -6315,8 +6335,19 @@ except: if globals.processingOptions.getCurrentOption("SectionsExpand"): createExpandingSections(prs) +# Maybe call an exit before the presentation is saved +onPresentationBeforeSave = globals.processingOptions.getCurrentOption("onPresentationBeforeSave") +if onPresentationBeforeSave != "": + exec(open(onPresentationBeforeSave).read()) + prs.save(output_filename) +# Maybe call an exit after the presentation is saved +onPresentationAfterSave = globals.processingOptions.getCurrentOption("onPresentationAfterSave") +if onPresentationAfterSave != "": + exec(open(onPresentationAfterSave).read()) + + elapsed_time = time.time() - start_time print(