macOS 使用交流 QQ 群:658095824,V : ez-code
python 无法直接操作 doc,只能操作 docx;
win2com 可以将 doc 转化为 docx,可惜无法在 linux/macOS 下安装 win32com;
使用 Pages 打开 doc,可以导出到 docx;
然而手动导出注定是繁琐的,这时想到可以使用 AppleScript 来操作 Pages
代码实现
如下代码实现了将 pages 文件导出为 docx
tell application "Pages"
set thisDoc to open "/Users/user/Desktop/奥运会.pages"
export thisDoc to POSIX file "/Users/user/Desktop/a.docx" as Microsoft Word
delay 2
close thisDoc
end tell
关于 POSIX-style
很多用户出现各种问题,是因为 file path 前面没有添加 POSIX;
/Users/user/Desktop/a.docx
是一个 POSIX 风格路径;
Some scriptable apps are designed to work with POSIX-style paths, rather than AppleScript alias and file objects. Like a file object, a POSIX file object is not dynamic and can also refer to an item that doesn’t exist yet.
A POSIX file object is displayed as a slash-delimited path preceded by a POSIX file specifier, in the format shown in Listing 15-10.