如何使用Python-pptx选择保存目录?

3

我很新于Python,正在使用PPTX Python进行项目开发。

一切都很好,但我不知道如何选择保存文件的目录。

以下是我的代码:

from pptx import Presentation
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!"

prs.save('test.pptx')

它将保存在我的桌面上。我如何选择目录?

谢谢! PS:我正在使用Python 3.7。

2个回答

3
让我猜一下- Python脚本也在桌面上!

prs.save('test.pptx') 是一个相对路径。 因此,test.pptx将存储在与您的脚本相同的目录中。 如果您想要另一个位置,请使用绝对路径,例如prs.save('C:/Users/xyz/Desktop/data/test.pptx')

这个链接可能也会有所帮助!;)

1
非常感谢。绝对路径是我需要的概念,因为我制作了一个pptx工厂,必须将pptx保存在不同的文件夹中。 - Alex Daxe

1
def save(self, path_or_stream):
    """
    Save this presentation package to *path_or_stream*, which can be
    either a path to a filesystem location (a string) or a file-like
    object. for example save(self, 'C:\mypath'):
    """
    self.package.save(path_or_stream)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接