What I’m trying to do:
I like using Anvil Assets to store images for icons, etc. I’m trying to insert images in python-pptx slides but, I don’t understand how to use/prepare images using anvil.media sufficiently to get the correct type of object the python-pptx method is expecting
What I’ve tried and what’s not working:
See the code sample below… and when you’re done laughing… I know that the python-pptx function is expecting an image location so I suspect what I need to be doing is getting a url for the image stored in my assets
Code Sample:
@anvil.server.callable
def gen_pptx():
# ***** This is the file_id fetch and incrementation
coll = app_tables.pptx_files.search(tables.order_by("file_id", ascending=False))
if len(coll) == 0:
file_id = 100
else:
file_id = int(coll[0]['file_id']) + 1
# ***** Creating presentation object *****
prs = Presentation()
# ***** Creating slide layout *****
#slide_layout_zero = prs.slide_layouts[0] # seems to be a title slide
#slide_layout_one = prs.slide_layouts[1] # seems to be a title with bulleted list
#slide_layout_two = prs.slide_layouts[2] # weird layout
#slide_layout_three = prs.slide_layouts[3] # title with two side-by-side bulleted lists
#slide_layout_four = prs.slide_layouts[4] # same as above but each list has a title
#slide_layout_five = prs.slide_layouts[5] # just a title
blank_slide_layout = prs.slide_layouts[6]
'''
slide_zero = prs.slides.add_slide(slide_layout_zero)
slide_one = prs.slides.add_slide(slide_layout_one)
slide_two = prs.slides.add_slide(slide_layout_two)
slide_three = prs.slides.add_slide(slide_layout_three)
slide_four = prs.slides.add_slide(slide_layout_four)
slide_five = prs.slides.add_slide(slide_layout_five)
'''
first_slide = prs.slides.add_slide(blank_slide_layout)
left = Inches(.5)
top = Inches(.3)
width = height = Inches(1)
txBox = first_slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
p = tf.paragraphs[0]
run = p.add_run()
run.text = "Do Now Recommendations"
font = run.font
font.name = "Calibri"
font.size = Pt(24)
font.bold = True
font.color.rgb = RGBColor(21, 44, 84)
bites = anvil.BlobMedia(content_type="bytes",content="_/theme/warning_yellow.jpg", name=None)
file = bites.url
warning_img = first_slide.shapes.add_picture(file, Inches(0), Inches(.3), height=Inches(1))
# ***** Saving The File *****
fname = 'test.pptx'
with anvil.media.TempFile() as file:
prs.save(file)
media_obj = anvil.media.from_file(file,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",fname)
rec = app_tables.pptx_files.add_row(file_id=file_id, file=media_obj)
return file_id
Clone link:
share a copy of your app