从Python调用LibreOffice时出错

mar*_*ing 3 python subprocess converter libreoffice

调用LibreOffice将文档转换为文本...

这可以从linux命令行正常工作:

soffice --headless --convert-to txt:"Text" document_to_convert.doc
Run Code Online (Sandbox Code Playgroud)

但是当我尝试从Python运行相同的命令时出现错误:

subprocess.call(['soffice', '--headless', '--convert-to', 'txt:"Text"', 'document_to_convert.doc'])
Run Code Online (Sandbox Code Playgroud)

错误:请重新验证输入参数...

如何从Python运行命令?

And*_*ini 5

这是您应该使用的代码:

subprocess.call(['soffice', '--headless', '--convert-to', 'txt:Text', 'document_to_convert.doc'])
Run Code Online (Sandbox Code Playgroud)

这是您发布的同一行,没有引号txt:Text.

你为什么看到这个错误?简单地说:因为soffice不接受txt:"Text".它只接受txt:Text.

为什么它在shell上运行?你的shell隐式删除了参数周围的引号,因此执行的命令实际上是:

soffice --headless --convert-to txt:Text document_to_convert.doc
Run Code Online (Sandbox Code Playgroud)

尝试运行此命令:

soffice --headless --convert-to txt:\"Text\" document_to_convert.doc
Run Code Online (Sandbox Code Playgroud)

不会删除引号,您将看到使用Python获得的验证输入参数消息.