cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
neelsrejan
Level III

Python error in automated powerpoint creation

Hi everyone,

 

I have a JSL script that outputs about 10 graphs in individual windows. I am working on a way to input each graph into its own slide in a powerpoint. Due to the complexities of the pictures I saw there was a way to use python's python-pptx package to accomplish this and I am able to create all the slides with a title but am unable to get the picture into the powerpoint at all. Below is the code snippet I am working with and the line that is commented out is the line that it's breaking at. I know this since the commented out line puts the picture and powerpoint in the right folder and opens seamlessly. But when uncommented the picture is saved but the following line where the ppt gets saved to the file folder never happens and likewise Jmp says it can't open the non-existant file. I have tried everything from using f strings to concatenations of the string to get the picture in the ppt but am unable. Any help would be much appreciated. Thanks!

dt = Open("$SAMPLE_DATA/Big Class.jmp");
biv = dt << Bivariate( Y( :height ), X( :weight ) );

ppt_dir = "C:\Users\neel\Documents\new jmp\";
number = "12345678";

graph holding = H List Box();
graph holding << append(Report(biv));
pic holding = H List Box();
pic holding << append(graph holding << get picture);
pic_file = "example.png";
pic holding << Save Picture(ppt_dir || pic_file);

Python Init();
plog = Python Execute({ppt_dir, number, pic_file}, {img_path, left, top, height},
"\[
from pptx import Presentation
from pptx.util import Inches

img_path = ppt_dir + pic_file

prs = Presentation()

slide_1 = prs.slides.add_slide(prs.slide_layouts[0])
slide_1.shapes.title.text = "Review of " + number

slide_2 = prs.slides.add_slide(prs.slide_layouts[5])
slide_2.shapes.title.text = "Results"

slide_3 = prs.slides.add_slide(prs.slide_layouts[5])
slide_3.shapes.title.text = "Summary"

left = top = Inches(2)
#pad_summary_pic = slide_3.shapes.add_picture(img_path, left, top, height=height)

prs.save(rf"{ppt_dir}{number} Review.pptx")
]\"
);
Python Term();

//Open PPT
ppt = ppt_dir || number || " Review.pptx";
Open(ppt);

Above works without the picture being added.

dt = Open("$SAMPLE_DATA/Big Class.jmp");
biv = dt << Bivariate( Y( :height ), X( :weight ) );

ppt_dir = "C:\Users\neel\Documents\new jmp\";
number = "12345678";

graph holding = H List Box();
graph holding << append(Report(biv));
pic holding = H List Box();
pic holding << append(graph holding << get picture);
pic_file = "example.png";
pic holding << Save Picture(ppt_dir || pic_file);

Python Init();
plog = Python Execute({ppt_dir, number, pic_file}, {img_path, left, top, height},
"\[
from pptx import Presentation
from pptx.util import Inches

img_path = ppt_dir + pic_file

prs = Presentation()

slide_1 = prs.slides.add_slide(prs.slide_layouts[0])
slide_1.shapes.title.text = "Review of " + number

slide_2 = prs.slides.add_slide(prs.slide_layouts[5])
slide_2.shapes.title.text = "Results"

slide_3 = prs.slides.add_slide(prs.slide_layouts[5])
slide_3.shapes.title.text = "Summary"

left = top = Inches(2)
pad_summary_pic = slide_3.shapes.add_picture(img_path, left, top, height=height)

prs.save(rf"{ppt_dir}{number} Review.pptx")
]\"
);
Python Term();

//Open PPT
ppt = ppt_dir || number || " Review.pptx";
Open(ppt);

This one with the add_picture uncommented doesn't.

 

Thanks,

Neel

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
neelsrejan
Level III

Re: Python error in automated powerpoint creation

Thank you all for the suggestions and help! Through troubleshooting using just python I realized that both the file path and height variable were preventing the file getting saved and importing the picture into the ppt. Below is the working code in case anyone ever needs to reference this!

dt = Open("$SAMPLE_DATA/Big Class.jmp");
biv = dt << Bivariate( Y( :height ), X( :weight ) );

ppt_dir = "C:\Users\srejann\Documents";
number = "12345678";

graph holding = H List Box();
graph holding << append(Report(biv));
pic holding = H List Box();
pic holding << append(graph holding << get picture);
pic_file = "example.png";
pic holding << Save Picture(ppt_dir || "\" || pic_file);

Python Init();
plog = Python Execute({ppt_dir, number, pic_file}, {img_path, left, top},
"\[
from pptx import Presentation
from pptx.util import Inches

img_path = ppt_dir + f"\{pic_file}"

prs = Presentation()

slide_1 = prs.slides.add_slide(prs.slide_layouts[0])
slide_1.shapes.title.text = "Review of " + number

slide_2 = prs.slides.add_slide(prs.slide_layouts[5])
slide_2.shapes.title.text = "Results"

slide_3 = prs.slides.add_slide(prs.slide_layouts[5])
slide_3.shapes.title.text = "Summary"

left = top = Inches(2)
pic = slide_3.shapes.add_picture(img_path, left, top)

prs.save(rf"{ppt_dir}\{number} Review.pptx")
]\"
);
Python Term();

//Open PPT
ppt = ppt_dir || "\" || number || " Review.pptx";
Open(ppt);

View solution in original post

7 REPLIES 7
jthi
Super User

Re: Python error in automated powerpoint creation

Have you tried running it just as python code with no JMP at all (is this Python or JMP problem)? Any error messages? My initial guess would be that the path could be built wrong. To my knowledge, in Python it is usually good idea to use os.path — Common pathname manipulations — Python 3.11.5 documentation

-Jarmo
neelsrejan
Level III

Re: Python error in automated powerpoint creation

Hi Jarmo, Your suggestion that the path was wrong was indeed one of the two things wrong with the code above. I didn't realize that the \" in the end of the file path was actually preventing the code to properly saving the file in the right location. Thanks for the help!

Craige_Hales
Super User

Re: Python error in automated powerpoint creation

In addition to @jthi  comments: put the python code into a python try block and catch and handle any exception. JMP's handling of the exception is hiding it from you. Running the python code outside of JMP will also work.

Browser Scripting with Python Selenium  shows a usage pattern for the try/except that I found helpful; it returns rc as either ok or the exception message text. Putting the import statements inside the try was really useful.

 

Craige
neelsrejan
Level III

Re: Python error in automated powerpoint creation

Thank you for the suggestion. I will look into more ways to catch exceptions/errors thrown by python through jmp. I appreciate the help!

mmarchandTSI
Level V

Re: Python error in automated powerpoint creation

This (highlighted) looks like it could be the issue to me.  Perhaps rename the variable "height" to avoid the conflict with the parameter "height"

 

mmarchandTSI_0-1695813760139.png

 

neelsrejan
Level III

Re: Python error in automated powerpoint creation

Hi, I appreciate the response. Your solution to remove height was one of the two things I realized was wrong with the script. Thank you for the suggestion to my oversight!

neelsrejan
Level III

Re: Python error in automated powerpoint creation

Thank you all for the suggestions and help! Through troubleshooting using just python I realized that both the file path and height variable were preventing the file getting saved and importing the picture into the ppt. Below is the working code in case anyone ever needs to reference this!

dt = Open("$SAMPLE_DATA/Big Class.jmp");
biv = dt << Bivariate( Y( :height ), X( :weight ) );

ppt_dir = "C:\Users\srejann\Documents";
number = "12345678";

graph holding = H List Box();
graph holding << append(Report(biv));
pic holding = H List Box();
pic holding << append(graph holding << get picture);
pic_file = "example.png";
pic holding << Save Picture(ppt_dir || "\" || pic_file);

Python Init();
plog = Python Execute({ppt_dir, number, pic_file}, {img_path, left, top},
"\[
from pptx import Presentation
from pptx.util import Inches

img_path = ppt_dir + f"\{pic_file}"

prs = Presentation()

slide_1 = prs.slides.add_slide(prs.slide_layouts[0])
slide_1.shapes.title.text = "Review of " + number

slide_2 = prs.slides.add_slide(prs.slide_layouts[5])
slide_2.shapes.title.text = "Results"

slide_3 = prs.slides.add_slide(prs.slide_layouts[5])
slide_3.shapes.title.text = "Summary"

left = top = Inches(2)
pic = slide_3.shapes.add_picture(img_path, left, top)

prs.save(rf"{ppt_dir}\{number} Review.pptx")
]\"
);
Python Term();

//Open PPT
ppt = ppt_dir || "\" || number || " Review.pptx";
Open(ppt);