1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

docxファイルをターミナルからコンバートする

Last updated at Posted at 2021-09-20

はじめに

大量のdocxファイルをtxtやhtmlに変換する場合、いちいちMSOfficeを使っていたら大変ですよね。
macOSやLinuxサーバで処理させたい時もあると思います。
macOSやLinuxのshellからコマンドでできればcronで処理もできるし、色々便利。
ここではLibreOfficeとunoconvを使った方法を紹介します。

動作確認環境

macOS BigSur(11.2)
Ubuntu 20.04

unoconvのインストール

ユニバーサルオフィスコンバータ unoconv
version 0.9.0
unoconv自体はpythonスクリプトのようです。

4年ぐらいメンテされていませんが動きます。

macの場合
$ brew install unoconv
linuxの場合
$ sudo apt-get install unoconv

LibreOffice

version 7.2.1.2

macの場合はインストーラをダウンロードして、そのままインストールでOK。
Ubuntu20.04の場合はLibereOffice 6.4.7.2が最初からインストールされています。

↓ここの説明だと「LibreOfficeの新しいバージョンは現在Mac用のunoconvと互換性がなく、LibreOffice 4.2をインストールする必要があります」とありますが、macOS Big Surでは4.2がクラッシュしますので最新版の7.2をインストールしてください。

unoconvコマンド

.docxを.txtに変換
shell
$ unoconv -f txt hoge.docx
.docxを.htmlに変換
shell
$ unoconv -f html hoge.docx
.docxを.pdfに変換

※pdfへも変換できますが、体裁がくずれます。

shell
$ unoconv -f pdf hoge.docx

rubyからはsystemコマンドで呼び出せます。

ruby
$ system("unoconv -f txt hoge.docx")
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?