Python's subprocess does not work as intended when multiple parameters are passed to launch the program

Asked 2 years ago, Updated 2 years ago, 301 views

The environment is Windows 10, Python 3.6.5.
I'm thinking of running a successful command prompt on Python (for future additional files)

soffice--headless--convert-toodes--infilter="Lotus:60"./123/test.123

This command converts the .123 file to a .odes file using the software command.
infilter="Lotus:60" accepts Japanese as input, converts the extension to ods, and prints the file.

We have verified the operation so far, and the files are printed in Japanese.

Here is the code you tried to execute this command on Python.

import subprocess

path_to_sooffice='C:/Program Files/LibreOffice/program/sooffice'#sooffice path
subprocess.run([path_to_office, '--headless', '--convert-to', 'odes', '--infilter="Lotus:60", './123/test.123', check=True)

The execution itself works well, but the infilter doesn't seem to work well, so the file will be printed in the default language of soffice (probably Italian?).

python windows character-code

2022-09-30 21:58

1 Answers

If you specify parameters in the list, it appears that " is converted to \".

--infilter="Lotus:60" will be used as --infilter=\"Lotus:60\", so it will no longer be considered a valid parameter.

If there are no specific defects, why don't you combine the first parameters of subprocess.run() into a single string as follows?

subprocess.run('C:/Program Files/LibreOffice/program/office' --headless --convert-toodes --infilter="Lotus:60"./123/test.123', check=True)


2022-09-30 21:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.