【问题标题】:Applescript Excel 2016 save as PDFApplescript Excel 2016 另存为 PDF
【发布时间】:2016-09-20 13:22:20
【问题描述】:

我正在尝试将 Excel 2016 电子表格另存为 PDF 文件。我有以下从 Objective C 程序中调用的简单 Applescript:

on saveExcelAsPDF(documentPath, PDFPath)
tell application "Microsoft Excel"
    open file documentPath
    save active sheet in PDFPath as PDF file format
    close active workbook saving no
    quit
end tell
end saveExcelAsPDF

此脚本在使用 Excel 2008 和 2011 时效果很好,但在使用 Excel 2016(版本 15.22)时失败。打开和保存命令都以不同的方式失败。有人可以帮帮我吗!我花了几个小时试图让它发挥作用。我已经阅读了我能找到的关于这个主题的所有帖子。我什至尝试使用“系统事件”来模仿击键。我尝试过的任何东西都不起作用。任何建议将不胜感激。谢谢!!

【问题讨论】:

  • 什么是documentPath 以及如何从 ObjC 调用脚本?
  • @vadian documentPath 和 PDFPath 都是 HFS 格式的字符串。
  • 至少尝试删除开放行中的file 或尝试open (documentPath as alias)
  • 我试过了,我尝试使用“别名”
  • 也试试open workbook workbook file name documentPath

标签: excel macos applescript


【解决方案1】:

这是 Excel 2016(版本 15.22) 的脚本。

我在脚本中添加了 cmets:

on saveExcelAsPDF(documentPath, PDFPath) -- params = two HFS paths
    set tFile to (POSIX path of documentPath) as POSIX file -- get a posix file object to avoid grant access issue with 'Microsoft Office 2016',  not the same as (file documentPath) when using the 'open  ...' command

    tell application "Microsoft Excel"
        set isRun to running
        set wkbk1 to open workbook workbook file name tFile
        alias PDFPath -- This is necessary to any script for 'Microsoft Office 2016', this avoid errors with any "save ... " command
        save workbook as wkbk1 filename PDFPath file format PDF file format with overwrite
        close wkbk1 saving no
        if not isRun then quit
    end tell
end saveExcelAsPDF

【讨论】:

  • 哇!!!这很好用!非常感谢。你是怎么想出来的?!?!?再次,谢谢!!
  • 由于我自己的脚本在所有情况下都不起作用,因此授予访问问题以打开不在最近文档列表中的文件,当目标是新的时在任何 save... 命令上发出文件或目标位于另一个文件夹中。我必须找到一个解决方案,所以我测试了一个简单的脚本并对其进行了修改,直到它可以正常工作。
【解决方案2】:

最后我做了文字、powerpoints和excel作品

1. From excel to pdf in batches

    on run
    set theseFiles to (choose file of type {"com.microsoft.excel.xls", "org.openxmlformats.spreadsheetml.sheet"} ¬
      with prompt "Choose the Excel sheets to export to PDF:" with multiple selections allowed)
    -- display dialog "theseItems: " & theseItems
    repeat with thisFile in theseFiles
    tell application "Finder"
    set theItemParentPath to container of (thisFile as alias) as text
    set theItemName to (name of thisFile) as string
    set theItemExtension to (name extension of thisFile)
    set theItemExtensionLength to (count theItemExtension) + 1
    set theOutputPath to theItemParentPath & (text 1 thru (-1 - theItemExtensionLength) of theItemName)
    set theOutputPath to (theOutputPath & ".pdf")
    end tell
    tell application "Microsoft Excel"
    set isRun to running
    activate
    open thisFile
    tell active workbook
    alias theOutputPath
    -- set overwrite to true
    save workbook as filename theOutputPath file format PDF file format with overwrite
    --save overwrite yes
    close saving no
    end tell
    -- close active workbook saving no
    if not isRun then quit
    end tell
    end repeat
    end run

2. From word to pdf in batches

    on run
    set theseFiles to (choose file of type {"com.microsoft.word.doc", "org.openxmlformats.wordprocessingml.document"} with prompt "Choose the Word documents to export to PDF:" with multiple selections allowed)
    -- display dialog "theseItems: " & theseItems
    repeat with thisFile in theseFiles
    tell application "Finder"
    set theItemParentPath to container of (thisFile as alias) as text
    set theItemName to (name of thisFile) as string
    set theItemExtension to (name extension of thisFile)
    set theItemExtensionLength to (count theItemExtension) + 1
    set theOutputPath to theItemParentPath & (text 1 thru (-1 - theItemExtensionLength) of theItemName)
    set theOutputPath to (theOutputPath & ".pdf")
    end tell
    tell application "Microsoft Word"
    --set default file path file path type documents path path theItemParentPath
    open thisFile
    --delay 1
    --set theActiveDocument to active document
    --save as theActiveDocument file format format PDF file name theOutputPath
    --close theActiveDocument
    tell active document
    save as file format format PDF file name theOutputPath
    close
    end tell
    end tell
    end repeat
    end run



3. From ppt to pdf in batches

    on run
    set theseFiles to (choose file of type {"com.microsoft.powerpoint.ppt", "org.openxmlformats.presentationml.presentation"} with prompt "Choose the PowerPoint Presentations to export to PDF:" with multiple selections allowed)
    -- display dialog "theseItems: " & theseItems
    repeat with thisFile in theseFiles
    tell application "Finder"
    set theItemParentPath to container of (thisFile as alias) as text
    set theItemName to (name of thisFile) as string
    set theItemExtension to (name extension of thisFile)
    set theItemExtensionLength to (count theItemExtension) + 1
    set theOutputPath to theItemParentPath & (text 1 thru (-1 - theItemExtensionLength) of theItemName)
    end tell
    tell application "Microsoft PowerPoint"
    open thisFile
    tell active presentation
    save in theOutputPath as save as PDF
    close
    end tell
    end tell
    end repeat
    end run

【讨论】:

    猜你喜欢
    • 2015-11-17
    • 2016-07-16
    • 2017-03-21
    • 1970-01-01
    • 2020-12-15
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    相关资源
    最近更新 更多