Search code examples
python-3.xcommand-linesubprocesslibreoffice

Access denied when executing a command via subprocess


I want to use LibreOffice to convert a file to pdf. I can do it alright from the command line, but I haven't been able to make the call from python. I'm on a Mac and there are two steps:

  1. Go to the libreoffice's folder (usually at /Applications/LibreOffice.app/Contents/MacOS)
  2. Execute ./soffice --headless --convert-to pdf input_folder--outdir output_folder

I tried

subprocess.run(["cd", libreoffice_path])  
subprocess.run(["./","soffice","--headless","--convert-to", "pdf", input_folder, "--outdir", output_folder])

but I'm getting a Permission denied: './' error.

I thought it was an access error and made soffice executable (although I suspect it already was) by using chmod +x from the command line. Still no luck.

What am I missing?


Solution

  • You split the first argument into two. Try running:

    subprocess.run(["./soffice", "--headless", "--convert-to", "pdf", input_folder, "--outdir", output_folder])
    

    also, a better way to change the working directory is to use os.chdir:

    import os
    os.chdir(libreoffice_path)