0

I'm running the following code on my windows machine using python's subprocess module.

import subprocess

args = ["abiword --to=pdf '{}'".format('test.docx')]
process = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, timeout=None)
print(process.stdout.decode())
filename = re.search('-> (.*?) using filter', process.stdout.decode())
print(filename.group(1))

But subprocess.run gives the following error:

b'\'"abiword --to=pdf \'test.docx\'"\' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n'

How to resolve this error and how should I proceed now? Also, is it correct to use abiword command in my windows machine? I want to convert my docx to pdf without installing any third party software like libreoffice.

Filip
  • 1,008
  • 6
  • 12
ninjacode
  • 318
  • 2
  • 18
  • Is `abiword` installed on your machine and is it available in the PATH? Said differently, what happens if you type `abiword --to-pdf test.docx` in a aconsole CMD.exe window? – Serge Ballesta Nov 26 '21 at 11:07
  • yes I have installed abiword on my win machine and the Path variable has been updated with the .exe file path 'C:\Program Files (x86)\AbiWord\bin'. I don't see any error when I do `abiword --to-pdf test.docx` on cmd but I don't see any pdf getting created. – ninjacode Nov 29 '21 at 07:28
  • @k-j It's 2.9.4 – ninjacode Nov 30 '21 at 05:01
  • I installed 2.8.4 but that also didn't work. The command line doesn't generate any pdf. I used Libreoffice but that doesn't maintain the alignment of tables and images. Don't know what other module should I use. – ninjacode Dec 01 '21 at 08:47

2 Answers2

2

You have 2 different problems here.

First one will be simple to solve: you do not give the correct parameters to subprocess. The first parameter (args) can be either a string which contains the full command line or a list containing the command and parameters as separate elements. So you should use either:

args = "abiword --to=pdf '{}'".format('test.docx')

(a simple string and not a list) or:

args = ["abiword", "--to=pdf",  '{}'.format('test.docx')]

The second one is that until you can generate the pdf by entering the abiword --to-pdf command in a console CMD.exe window, the same command launched with subprocess.run will not give better results...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • `'{}'.format('test.docx')` is just a wicked complicated way to write `'test.docx'` – tripleee Nov 29 '21 at 08:23
  • @tripleee: You are right, but I did not want to change that part because it is fully unrelated to OP problem. – Serge Ballesta Nov 29 '21 at 08:43
  • 1
    On Windows, the difference between `shell=True` or not is smaller, but for the record, the only _portable_ way to use the second form is to also remove the `shell=True`. Perhaps see also [Actual meaning of `shell=True` in subprocess](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess) – tripleee Nov 29 '21 at 08:49
  • Probably also add `check=True` to have Python raise an exception if `abiword` fails for some reason. – tripleee Nov 29 '21 at 08:50
  • @serge-ballesta The command is not throwing any error but it's not generating the PDF as well. I tried another method to convert docx to pdf using Libreoffice and it worked. – ninjacode Nov 29 '21 at 09:15
-2

Use the full path to abiword: /usr/bin/abiword

Glenn
  • 7,262
  • 1
  • 17
  • 23
  • That would help if the command they were trying to execute was actually `abiword`, but as per the error message, it's not. – tripleee Nov 29 '21 at 08:47