【问题标题】:Controlling PowerPoint with Python's win32com. How to access "Save As" option programmatically使用 Python 的 win32com 控制 PowerPoint。如何以编程方式访问“另存为”选项
【发布时间】:2020-05-21 04:51:00
【问题描述】:

我正在尝试通过 python 打开 powerpoint,然后将幻灯片演示文稿保存为 pdf 讲义(三到一页)。经过一番谷歌搜索,我偶然发现了this。一个非常相似的问题。但是,我似乎无法让它运行。我想我可能错误地将VB“翻译”成Python。

查看 MSDN 文档here,我尝试填写两个必需的参数作为起点。虽然它似乎接受path,但它会抛出FixedFormatType 的错误。

我的代码如下。

import win32com.client, sys, os

app = win32com.client.Dispatch("PowerPoint.Application")
app.Visible = True

names = []
for i in os.listdir(os.getcwd()):
    if i[-1] == 't':
        tmp = os.getcwd() + '\\' + i
        names.append(tmp)
        tmp = ''



presentation = app.Presentations.Open(names[0])
presentation.ExportAsFixedFormat(os.getcwd() + '\\test.pdf', 'PpFixedFormatType')

我尝试了ppFixdFormatType 参数的许多变体。 ppFixedFormatTypePDFwin32com.ppFixedFormatTypePDFwin32com.client.constants.ppFixedFormatTypePDF 等。但都会引发一种或另一种类型的错误。大部分是这样的:ValueError: invalid literal for int() with base 10: 'PpFixedFormatType'

【问题讨论】:

  • 我不知道python但是试试这个presentation.ExportAsFixedFormat(os.getcwd() + '\\test.pdf', 2)
  • @SiddharthRout -- 不走运 :( 我收到以下错误:TypeError: The Python instance can not be converted to a COM object
  • 其余参数如何? presentation.ExportAsFixedFormat(os.getcwd() + '\\test.pdf', 2, 0, 0, 1, 1, False, 1, False, False, False,False) 这只是一个例子。根据您的要求进行更改。
  • @Siddharth Rout -- 仍然没有运气。抛出相同的 COM 对象错误。
  • 您是否尝试过在 Google 中搜索 The Python instance cannot be converted to a COM object

标签: python vba client powerpoint win32com


【解决方案1】:

您可能在 PowerPoint 的 API 中遇到过这个错误:Type mismatch when using export function of PowerPoint 2007。 解决方案是为 ExportAsFixedFormat() 调用指定 PrintRange 参数。

【讨论】:

    【解决方案2】:

    我知道问题是关于通过 VBA 操作 PowerPoint,但您也可以使用免费和开源的 pdftools 将 PDF 幻灯片保存为讲义(就像 PowerPoint 一样)。

    保存 3x3 讲义的命令是:

    pdftools --input-file slides.pdf --output slides_W.pdf --white-page --fitpaper --overwrite
    pdftools --input-file slides_W.pdf --nup 3 3 --height 0.32 --delta 0mm 0mm --landscape --out-suffix _HANDOUT --frame --overwrite --paginate --paper a4paper -t $filename 0.4 0.01
    

    您可以通过操作传递给--nup选项的参数来获得不同的讲义布局

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 2021-07-09
      • 1970-01-01
      • 2011-01-03
      相关资源
      最近更新 更多