使用Applescript将Excel转换为PDF(另存为PDF)

时间:2016-03-29 09:00:51

标签: excel pdf applescript applescript-excel

我有一个.xlsx文件,我希望使用Applescript将其保存为PDF格式。我已经彻底搜索了这个,但显然有一百万个问题。

我希望保存为.app,其中代码中的...将被代码块替换,以将活动工作簿转换为PDF,如下所示:

tell application "Microsoft Excel"
    activate
    open  "/somedirectory/workbook1.xlsx"
    ...
end tell

1 个答案:

答案 0 :(得分:-1)

发现它!这是答案:

http://blog.krugazor.eu/2010/01/applescript-batch-saving-to-pdf/

以下链接引用了以下内容:

"基本上,它的作用是模拟击键和点击。这使您可以执行用户可以执行的任何操作。

没有进一步的麻烦,AppleScript使用打印对话框批量保存为pdf一堆excel文件!"

on open some_items
    repeat with this_item in some_items
        try
            tell application "Microsoft Excel"
                activate
                open this_item
                tell application "System Events" to tell process "Microsoft Excel"
                    --Bring up print window
                    keystroke "p" using command down
                    --Choose "PDF" > "Save as PDF"
                    click (menu button "PDF" of window 1)
                    click (menu item 1 of menu of menu button "PDF" of window 1)
                    delay 2
                    -- Choose the desktop as save locaton (Command-D)
                    keystroke "d" using command down
                    --Save
                    keystroke "s" using command down
                    keystroke return

                    -- wait and close
                    delay 3
                    keystroke "w" using command down
                end tell
            end tell
        end try
    end repeat

    tell application "Finder"
        open desktop
    end tell
end open