I am having the most frustrating problem with libreoffice in Python
when I run the following in terminal I have no problem at all, the pdf file is produced where I want it and life is dandy:
cd /Applications/LibreOffice.app/Contents/MacOS/
./soffice --convert-to pdf --outdir {output_folder} {path_to_docx_file}/{title}.docx
However when I have tried to add this to my python script:
SOFFICE = r'/Applications/LibreOffice.app/Contents/MacOS/soffice'
subprocess.Popen([SOFFICE, "--convert-to", "pdf", "--outdir", "{output_folder} ", "{path_to_docx_file}/{title}.docx"])
I get an error saying :
Error: source file could not be loaded
I have tried opening up all the permissions of all of the binaries and files and this still doesn't work in the python script. What am I doing wrong?
It's because you need to change the current working directory, not just give an absolute path to the command.
Should be replaced with:
Even if it seems to be quite similar, there's a major difference between those two calls: the current working directory.
With the script:
If you're calling the python script in the ~ directory, it will try to reach ~/file.docx.
But, in the second one :
It will try to reach the file in "/Applications/LibreOffice.app/Contents/MacOS/file.docx", which is the same behaviour of what you're doing with the
cd
command (in fact, the cd command changes the current directory, so giving the cwd argument is the same as making acd
call).You can also use absolute paths for all your files and it will solve the problem too but it's not what you're trying to do. It depends on the software you are trying to build and it's purpose.
It's why the prompt says that the file does not exist. The program can't find the file in
WHERE_YOU_CALL_THE_SCRIPT/{path_to_docx_file}/{title}.docx
because I suppose that the file is in/Applications/LibreOffice.app/Contents/MacOS/{path_to_docx_file}/{title}.docx
.