通过 CherryPy 提供 pptx 文件

Serve pptx file through CherryPy

我试图让用户在访问 pge 时下载 pptx 文件,比方说:http://127.0.0.1:8080/download

这是我的代码片段:

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")

我不太明白 serve_file 是如何工作的,想知道这样做是否正确。 我是 CherryPy 的初学者。
希望你能帮帮我。

只是更新。我用过 static.serve_file 并且有效!

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