2016-06-12 114 views
1

我有在Python使用Python内soffice,命令工作的终端,但没有在Python子

当我运行在终端下面

与LibreOffice中最令人沮丧的问题,我有一点问题都没有,PDF文件生产,我想它,生活是花花公子:

cd /Applications/LibreOffice.app/Contents/MacOS/ 

./soffice --convert-to pdf --outdir {output_folder} {path_to_docx_file}/{title}.docx 

然而,当我试图把它添加到我的python脚本:

SOFFICE = r'/Applications/LibreOffice.app/Contents/MacOS/soffice' 

subprocess.Popen([SOFFICE, "--convert-to", "pdf", "--outdir", "{output_folder} ", "{path_to_docx_file}/{title}.docx"]) 

我收到一条错误消息:

Error: source file could not be loaded

我试图打开所有的二进制文件和文件的所有权限,这仍然不能在python脚本中工作。我究竟做错了什么?

回答

1

这是因为您需要更改当前工作目录,而不仅仅是给出命令的绝对路径。

subprocess.Popen(["/Applications/LibreOffice.app/Contents/MacOS/soffie", "--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

相关问题