LibreOfficeをコマンドラインで使うと便利だよ
LibreOffice Advent Calendar 2015 21日目。本日も超小ネタ。
みんな知ってるのかなーと思ったのですが、意外と知らない人がいるようなので*1。
LibreOfficeのいろんな機能は実はコマンドラインから起動できます。
Ubuntu*2の場合は実行パスが通っているので、単純に実行できます。
$ soffice --help
LibreOffice 5.0.2.2 00m0(Build:2)
Usage: soffice [options] [documents...]
Options:
--minimized keep startup bitmap minimized.
--invisible no startup screen, no default document and no UI.
--norestore suppress restart/restore after fatal errors.
--quickstart starts the quickstart service
--nologo don't show startup screen.
--nolockcheck don't check for remote instances using the installation
--nodefault don't start with an empty document
--headless like invisible but no user interaction at all.
--help/-h/-? show this message and exit.
--version display the version information.
--writer create new text document.
--calc create new spreadsheet document.
--draw create new drawing.
--impress create new presentation.
--base create new database.
--math create new formula.
--global create new global document.
--web create new HTML document.
-o open documents regardless whether they are templates or not.
-n always open documents as new files (use as template).
--display <display>
Specify X-Display to use in Unix/X11 versions.
-p <documents...>
print the specified documents on the default printer.
--pt <printer> <documents...>
print the specified documents on the specified printer.
--view <documents...>
open the specified documents in viewer-(readonly-)mode.
--show <presentation>
open the specified presentation and start it immediately
--accept=<accept-string>
Specify an UNO connect-string to create an UNO acceptor through which
other programs can connect to access the API
--unaccept=<accept-string>
Close an acceptor that was created with --accept=<accept-string>
Use --unnaccept=all to close all open acceptors
--infilter=<filter>[:filter_options]
Force an input filter type if possible
Eg. --infilter="Calc Office Open XML"
--infilter="Text (encoded):UTF8,LF,,,"
--convert-to output_file_extension[:output_filter_name[:output_filter_options]] [--outdir output_dir] files
Batch convert files (implies --headless).
If --outdir is not specified then current working dir is used as output_dir.
Eg. --convert-to pdf *.doc
--convert-to pdf:writer_pdf_Export --outdir /home/user *.doc
--convert-to "html:XHTML Writer File:UTF8" *.doc
--convert-to "txt:Text (encoded):UTF8" *.doc
--print-to-file [-printer-name printer_name] [--outdir output_dir] files
Batch print files to file.
If --outdir is not specified then current working dir is used as output_dir.
Eg. --print-to-file *.doc
--print-to-file --printer-name nasty_lowres_printer --outdir /home/user *.doc
--cat files
Dump text content of the files to console
Eg. --cat *.odt
--pidfile file
Store soffice.bin pid to file.
-env:<VAR>[=<VALUE>]
Set a bootstrap variable.
Eg. -env:UserInstallation=file:///tmp/test to set a non-default user profile path.
Remaining arguments will be treated as filenames or URLs of documents to open
詳しくは個別に見ていただくとして、 --headless に注目。これを使うと、LibreOfficeのGUIを表示せずに機能を利用できます。ありがちなのは:
soffice --headless --convert-to pdf:writer_pdf_Export *.doc
とか。これでカレントディレクトリにある *.doc ファイルをガツンとPDFに変換できます。サーバーサイドに置いておいて、適当なファイルがアップロードされたらPDFに変換、みたいなサービスを提供するのに使えますね。
なお --headless オプションは、他にLibreOfficeが上がっている場合は何も言わずに終了してしまうのでご注意。
なおOS Xの場合は:
/Applications/LibreOffice.app/Contents/MacOS/soffice
を叩いてください。
Windowsは手元にないのでわかりませんが、32bit版は c:\Program Files(x86) 以下、64bit版は c:\Program Files 以下のLibreOfficeのフォルダ以下に program というフォルダがあって、そこに soffice.exe という実行ファイルがあるのでこれを叩きましょう。バッチファイルを書いておくといいかも。
--infilterや--convert-toのフィルター指定にはいろいろオプション指定も効くので、これについての情報もお知らせしたいところですが、情報源がないのでまた今度。すみません!
追記:このネタ、「LibreOffice コマンドライン」とかでググるとたくさん出てくるので、今更感もありますけど、Advent Calendarにぶら下がってる方が見つかりやすいよね、という言い訳。
*1:秋のOSC東京で紹介したら、すごい感激された。
*2:しか手元にないけど、他のLinuxディストリビューションでも同じなのだと思います。
