他の.odsファイルを表示すると、.odsに変換中に問題が発生する

他の.odsファイルを表示すると、.odsに変換中に問題が発生する

私のPythonソフトウェアはしばしば次のコマンドを使用します。

soffice --headless --convert-to ods --outdir /tmp tblIssues.csv
soffice --view /tmp/tblIssues.ods
rm /tmp/tblIssues.ods

私の質問に答えるCSVファイルを開き、スプレッドシートに直接移動する

現在、ユーザーは複数のファイルを同時に表示する機能を要求しています。問題は、ファイルを表示しても変換が再実行されないことです。エラーが報告されていないため、少し混乱しています。コマンドラインから変換を実行しようとするとハングします。

ベストアンサー1

コマンドラインまたはスクリプトでLibreOfficeを使用して目的の方法に変換し、他の操作も実行できます。

複数のファイルを変換してから表示したり(例1)、バックグラウンドでビューを開き、必要に応じて追加のファイルを変換したりできます(例2)。

これらの操作は、シリアルまたはパラレル(コルーチン)で実行できます。重要なのは、翻訳やその他のバックグラウンドプロセスの実行中に新しいユーザーコンテキストを作成し、それをTCPポートにバインドすることです。ユーザーの場所とポートが利用可能であれば、必要に応じて作成できます。

次の 2 つの例は、スクリプトに適した形式で提供されます。

説明-env:ユーザーのインストール1見つけることができるここと参照- 受け入れるテーブルで確認可能ここ。接続は次のとおりです。ソケットまたは管路(名前付きパイプが必要です)コマンドラインオプションの概要は次のとおりです。LibreOfficeドキュメント。 LibreOfficeのデフォルト設定は次の場所にあります。ブートローダー1そしてオフィス2

uno 文字列が必要なため、構文が少し複雑に見えることがあります。私の考えでは、uno文字列はよく文書化されていません。ただし、このアプローチの利点は、必要に応じて文書生成/処理システムを有効にするためにネットワーク経由で展開できることです。

例 1. 変換後の表示

次のソースコードは、まずこれを並列に実行する方法(各インスタンスに対して個別のユーザー/ポートバインディングが必要)とシリアルで実行する方法を示しています。

# My LibreOffice version is:

# $ libreoffice --version
# LibreOffice 4.2.8.2 420m0(Build:2)

# My files are in a local folder '.'

# $ ls .
# sample00.csv sample01.csv sample02.csv

# Parallel conversion: convert as many as you wish; to be done in parallel
# each instance will exit when completed.  You can set UserInstallation 
# to the location desired, as long as the user can access it, and you can 
# use any tcp port you wish, as long as it is available. The location and
# port are created if they do not exist. Note that each location and port
# must be unique. The location ~/.loports/<port number> is a convenient
# construct. An explanation of -env:UserInstallation and --accept follows.

# -env:UserInstallation=</absolute/path/to/unique/location>
# changes the default user location that is found in bootstraprc.

# --accept
# 'socket,' use a socket 
# 'host=0,' any host (also can use localhost or nnn.nnn.nnn.nnn).
# 'port=NNNN,' can be any available tcp port number.
# 'tcpNoDelay=1;' for uno connections, 0 for 200ms delay (default).

# Now do:

soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8100" --accept="socket,host=0,port=8100,tcpNoDelay=1;" --convert-to ods --outdir . sample00.csv &
soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8101" --accept="socket,host=0,port=8101,tcpNoDelay=1;" --convert-to ods --outdir . sample01.csv &
soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8102" --accept="socket,host=0,port=8102,tcpNoDelay=1;" --convert-to ods --outdir . sample02.csv &

# ... and so on ...

# To open multiple views, do:

soffice --nologo --view sample00.ods sample01.ods sample02.ods &

# ... and so on ... 

# Serial conversion:  you can do serial conversion by sending each csv
# file to the same port but you must wait for it to finish first.
# Each conversion will exit when completed. Do something like:

( soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8100" --accept="socket,host=0,port=8100,tcpNoDelay=1;" --convert-to ods --outdir . sample00.csv &&
  soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8100" --accept="socket,host=0,port=8100,tcpNoDelay=1;" --convert-to ods --outdir . sample01.csv &&
  soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8100" --accept="socket,host=0,port=8100,tcpNoDelay=1;" --convert-to ods --outdir . sample02.csv &&
  soffice --nologo --view sample00.ods sample01.ods sample02.ods ) &

# Quick note, when completed, check that your directory, in this case,
# ~/.loports, has been removed. 

例 2: ビュー後の変換 (バックグラウンドで)

ファイルの表示中にバックグラウンド変換が可能です。 LibreOfficeの各インスタンスに対して新しいユーザーを作成し、新しいTCPポートをそのユーザーにバインドするだけです。以下は例です。

# Open a view:

soffice --nologo --view sample00.ods sample01.ods sample02.ods &

# Then convert in the background as you like:

( soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8101" --accept="socket,host=0,port=8100,tcpNoDelay=1;" --convert-to ods --outdir . sample00.csv &&
  soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8101" --accept="socket,host=0,port=8100,tcpNoDelay=1;" --convert-to ods --outdir . sample01.csv &&
  soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8101" --accept="socket,host=0,port=8100,tcpNoDelay=1;" --convert-to ods --outdir . sample02.csv ) &

ファイルはシリアルに変換されるため、同じポートにバインドできます。あなたが見ているファイルがバックグラウンドでも変換されていることがわかります。ただし、並列に変換する場合は、各コンバータを一意のTCPポートにバインドする必要があります。もちろん、ユニークで利用可能な限り、すべてのユーザー/ポートを使用できます。

最後に...

追加のスプレッドシートやソフトウェアを使用せずに、必要に応じてスクリプトでこのプロセスをスケジュールし、LibreOfficeが提供する幅広い変換フィルタを利用できます。

または、ooBasicを使用してLibreOfficeでこれを行うこともできます。たとえば、メニューオプションを使用して組み込みのインポートフィルタとエクスポートフィルタにアクセスできます。

追加ボーナスとして、バックグラウンド変換後に次の操作を実行すると(例2に従って)、LibreOfficeが強制的に更新され、新しく変換されたファイルがロードされます。

xdotool search --name sample00.ods windowactivate --sync key --clearmodifiers ctrl+shift+r &&
xdotool search --name sample01.ods windowactivate --sync key --clearmodifiers ctrl+shift+r &&
xdotool search --name sample02.ods windowactivate --sync key --clearmodifiers ctrl+shift+r

しかし、インストールする必要がありますxdoツールまず、次の操作を行いますsudo apt-get install xdotool

脚注

1基本ユーザーのインストール設定は次の場所にあります。/usr/lib/libreoffice/プログラム/bootstraprc

2一般的な構成設定は次の場所にあります。/etc/libreoffice/sofficerc

おすすめ記事