< ページ移動: 1 2 >
外部プログラムと通信する port 番号を指定して Calc を起動するので、メニュー等からではなく、コマンドで起動すること(Windows ならコマンドプロンプトからとか)C:\Users\shinoda\Source\Python>"C:\Program Files\LibreOffice\program\soffice.exe" --calc --norestore --accept=socket,host=localhost,port=8100;urpこれで、外部プログラムとの通信用 port(8100番)を開けた Calc が起動する。
今回起動する Python スクリプトはこんな感じ。カードリーダー SONY RC-S380 で読みとった FeliCa カード情報を Calc の任意のセルに貼り付けるだけのプログラム。
import nfcfrom nfc.clf import RemoteTargetimport unolocalContext = uno.getComponentContext()resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)# Calc に port=8100 で接続ctx = resolver.resolve( "uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext" )smgr = ctx.ServiceManager# ドキュメントオブジェクトの取得desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)doc = desktop.getCurrentComponent()sheet = doc.getSheets().getByName('Sheet1')# カード読み込み待ちclf = nfc.ContactlessFrontend('usb')