1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

rangerでPDFとMSワードとMSエクセルをターミナルでplain text形式で開いたりプレビューするスクリプト

Last updated at Posted at 2020-08-20

この記事ではrangerでPDF、MS office ワードとエクセルをテキスト形式でプレビュー表示する方法を紹介します。
rangerはPython製CLIファイラーです。

準備

  • rangerpopplercatdocdocx2txtxlsx2csvが必要です。
  • 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--
1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?