この記事ではrangerでPDF、MS office ワードとエクセルをテキスト形式でプレビュー表示する方法を紹介します。
rangerはPython製CLIファイラーです。
準備
- rangerとpopplerとcatdocとdocx2txtとxlsx2csvが必要です。
- MS Officeは必要ありません。
- Archlinuxユーザーなら、
pacman -S ranger catdoc docx2txt poppler
でPDFとワードのプレビューに必要なツールが手に入ります。 - エクセルプレビューに必要なツールはAURにあるので
yay -S python-xlsx2csv
Enterしたときにpdf/doc/docx/xls/xlsxファイルをplain text形式でターミナルで開く方法
開くアプリの選択は~/.config/ranger/rifle.conf
に書きます。
rifle.conf
--snip--
# -------------------------------------------
# Documents
# -------------------------------------------
--snip--
ext pdf, has pdftotext, terminal = pdftotext -layout -nopgbrk "$@" - | "$PAGER"
ext doc?, has catdoc, terminal = catdoc -- "$@" | "$PAGER"
ext docx?, has docx2txt, terminal = docx2txt "$@" - | "$PAGER"
ext xls?, has xls2csv, terminal = xls2csv -- "$@" | "$PAGER"
ext xlsx?, has xlsx2csv, terminal = xlsx2csv -- "$@" | "$PAGER"
--snip--
選択中にpdf/doc/docx/xls/xlsxファイルをplain text形式でターミナル上にプレビューするスクリプト
プレビュースクリプトは~/.config/ranger/scope.sh
に書きます。
scope.sh
--snip--
# wraps highlight to treat exit code 141 (killed by SIGPIPE) as success
highlight() { command highlight "$@"; test $? = 0 -o $? = 141; }
case "$extension" in
--snip--
# PDF documents:
pdf)
try pdftotext -l 10 -nopgbrk -q "$path" - && \
{ dump | trim | fmt -s -w $width; exit 0; } || exit 1;;
# DOC documents:
doc)
try catdoc "$path" && \
{ dump | trim | fmt -s -w $width; exit 0; } || exit 1;;
# DOCX documents:
docx)
try docx2txt "$path" - && \
{ dump | trim | fmt -s -w $width; exit 0; } || exit 1;;
# XLS documents:
xls)
try xls2csv "$path" && \
{ dump | trim | fmt -s -w $width; exit 0; } || exit 1;;
# XLSX documents:
xlsx)
try xlsx2csv "$path" && \
{ dump | trim | fmt -s -w $width; exit 0; } || exit 1;;
--snip--