问题描述
调用LibreOffice将文档转换为文本...
Calling LibreOffice to convert a document to text...
这在linux命令行上可以正常工作:
This works fine from the linux command line:
soffice --headless --convert-to txt:"Text" document_to_convert.doc
但是当我尝试从Python运行相同的命令时出现错误:
But I get an error when I try to run the same command from Python:
subprocess.call(['soffice', '--headless', '--convert-to', 'txt:"Text"', 'document_to_convert.doc'])
如何获取从Python运行的命令?
How do I get the command to run from Python?
推荐答案
这是您应使用的代码:
subprocess.call(['soffice', '--headless', '--convert-to', 'txt:Text', 'document_to_convert.doc'])
这是您发布的同一行,但在txt:Text
周围没有引号.
This is the same line you posted, without the quotes around txt:Text
.
为什么会看到错误?简而言之:因为办公室不接受txt:"Text"
.它仅接受txt:Text
.
Why are you seeing the error? Simply put: because soffice does not accept txt:"Text"
. It only accepts txt:Text
.
它为什么在shell上起作用?您的shell隐式删除了参数周围的引号,因此要执行的命令实际上是:
Why is it working on the shell? Your shell implicitly removes quotes around arguments, so that the command that gets executed is actually:
soffice --headless --convert-to txt:Text document_to_convert.doc
尝试运行此命令:
soffice --headless --convert-to txt:\"Text\" document_to_convert.doc
行情不会被删除,您会看到请验证输入参数消息,该消息是通过Python获得的.
Quotes won't be removed and you'll see the Please verify input parameters message you are getting with Python.
这篇关于从Python调用LibreOffice时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!