Search code examples
python-3.xcherrypypython-pptx

Serve pptx file through CherryPy


I was trying to let the user to download the pptx file when they visited a pge, let's say : http://127.0.0.1:8080/download

Here is a snippet of my code:

from pptx import Presentation
from cherrypy.lib.static import serve_file

@cherrypy.expose
def download(self):
    prs = Presentation()
    title_slide_layout = prs.slide_layouts[0]
    slide = prs.slides.add_slide(title_slide_layout)
    title = slide.shapes.title
    subtitle = slide.placeholders[1]
    title.text = "Hello, World!"
    subtitle.text = "python-pptx was here!"
    pptx = prs.save('test.pptx')
    return serve_file(path, "application/x-download", "attachment")

I do not really understand how serve_file works and wondering whether it is the right to do so. I am a beginner in CherryPy.
Hope you could help me out.


Solution

  • Just an update. I have used static.serve_file and it worked!

    return static.serve_file(path, "application/x-download",
                                 "attachment", name =os.path.basename(path))