这是因为您需要更改当前工作目录,而不仅仅是给出命令的绝对路径。
subprocess.Popen(["/Applications/LibreOffice.app/Contents/MacOS/soffice", "--convert-to", "pdf", "--outdir", "{output_folder} ", "{path_to_docx_file}/{title}.docx"])
应替换为:
subprocess.Popen(["soffice", "--convert-to", "pdf", "--outdir", "{output_folder} ", "{path_to_docx_file}/{title}.docx"], cwd="/Applications/LibreOffice.app/Contents/MacOS/")
即使看起来很相似,这两个调用之间也有一个主要区别:当前工作目录。
使用脚本:
subprocess.Popen(["/Applications/LibreOffice.app/Contents/MacOS/soffie", "--convert-to", "pdf", "--outdir", "{output_folder} ", "file.docx"])
如果您在 ~ 目录中调用 python 脚本,它将尝试访问 ~/file.docx。
但是,在第二个中:
subprocess.Popen(["soffice", "--convert-to", "pdf", "--outdir", "{output_folder} ", "file.docx"], cwd="/Applications/LibreOffice.app/Contents/MacOS/")
它将尝试访问“/Applications/LibreOffice.app/Contents/MacOS/file.docx”中的文件,这与您使用命令所做的行为相同cd
(实际上,cd 命令会更改当前目录,因此给出 cwd 参数与cd
拨打电话相同)。
您还可以对所有文件使用绝对路径,它也可以解决问题,但这不是您想要做的。这取决于您尝试构建的软件及其目的。
这就是提示说该文件不存在的原因。该程序无法找到该文件,WHERE_YOU_CALL_THE_SCRIPT/{path_to_docx_file}/{title}.docx
因为我认为该文件位于/Applications/LibreOffice.app/Contents/MacOS/{path_to_docx_file}/{title}.docx
.