簡體   English   中英

python 控制台命令檔腳本

[英]python console command stalls script

我的工作流程通常遵循這個鏈條:

  • 在 libreoffice 中使用 soffice 命令打開示例數據文件,這樣就可以連接示例數據文件了,這是一個控制台命令:

     $ soffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" /path/to/sample/data.ods
  • 運行 python 控制台並輸入以下連接命令:

     >>> sys.path.append("/path/to/uno_lib/.local/lib/python3") >>> import uno >>> local_context = uno.getComponentContext() >>> resolver = local_context.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local_context) >>> ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext") >>> smgr = ctx.ServiceManager >>> desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx) >>> document = desktop.getCurrentComponent()
  • 使用 uno 命令提取數據

     pseudo-code >>> document.getSheet("data").getCellRange("B5", "H15")

顯然,我想將所有這些放入文件“connect”和 function“connection”中,例如:

連接.py

import os
import uno
def connection():
    os.system(soffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" /path/to/sample/data.ods)
    context ...
    resolver
    ctx ...
    smgr ....
    desktop ...
    document ...
    return document

所以我可以簡單地 go:

$ python
>>> import connect
>>> document = connection()
>>> document.getSheet("DataSheet").getCellRange("B5", "H15")

哦,我多么喜歡這個:)

有問題:當我調用 function“connection()”時,控制台在打開 calc 文件的系統命令處停止,只有當我關閉 calc 文件時,腳本才會繼續,但它不再連接,因為文件是關閉:(我該如何規避這個?

首選方法是使用 shell 腳本而不是系統調用來啟動 LibreOffice。

./start_soffice_listening.sh
python
>>> import connect
>>> document = connection()

這是我使用的 shell 腳本:

if [ "$1" == "-headless" ]; then
    echo "Opening headless (use 'pkill loffice' and 'pkill soffice.bin' to end"
    loffice "--accept=socket,host=localhost,port=2002;urp;" --writer --headless --nologo --nofirststartwizard --nolockcheck --norestore &
else
    loffice "--accept=socket,host=localhost,port=2002;urp;" --writer &
fi

否則,您將需要線程和睡眠命令才能從 Python 中與操作系統進行交互。 這比直接啟動 LO 更困難且更不可靠。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM