Search

Nautilus - Thumbnailer for Ms Office documents

Contents[Hide]

dropcap-gnome-msoffice

When you use Gnome Shell under Ubuntu or Debian, all Ms Office files are displayed thru some generic icons in Nautilus.

With the help of LibreOffice unoconv conversion tool, it is possible to use the first page of a document as a Nautilus thumbnail.

To make it even more visual, the thumbnail can be outdrawn by the document type official icon. You will then get a display much more visual.

This article explains all the steps needed to configure Nautilus to display thumbnails of Ms Office documents including document type icon and first page content, providing a simple and efficient Ms Office documents preview. It is using LibreOffice conversion tools and some very basic tools.

This procedure has been tested under Ubuntu Gnome 14.04 LTS with Libre Office 4.2 & 5.0. It should be applicable to many Gnome based distributions. It has also been tested under Lubuntu 14.04 LTS with PCManFM.

Here is the display you get in Nautilus with the generic icons :

nautilus-msoffice-nothumbnail

And, here is the display you will get with the OpenOffice thumbnailer active :

nautilus-msoffice-thumbnail

If you just want to install the MsOffice thumbnailer on your system, without any specific explanation, you can jump to Thumbnailer Installation.

1. Pre-requisite

The thumbnailer script uses different tools :

  • unoconv (extension of the LibreOffice standard installation)
  • convert and composite (to convert and assemble final thumbnail)
  • mimetype (to determine Office file type)
  • gvfs-copy (to copy files from remote filesystem) 

All these tools can be easily installed under Ubuntu :

Terminal
# sudo apt-get install gvfs-bin libfile-mimeinfo-perl unzip unoconv imagemagick

2. Thumbnailer Script

When Nautilus needs to generate a thumbnail for a file, it is calling a 'thumbnailer'. This program (or script) is in charge of generating the thumbnail in a PNG format with a given height.

As Ms Office files are not providing an embedded thumbnail, thumbnail generation is done following few steps :

  1. convert first document page to PDF with unoconv
  2. convert PDF page to PNG with convert
  3. Get document mimetype with mimetype
  4. Determine icon mask accordingly
  5. Transform the PNG applying the specific mask with composite
  6. Resize final image to the thumbnail size provided by Nautilus

The following script is the Ms Office document thumbnailer in charge of this task.

/usr/local/sbin/lo-thumbnailer
#!/bin/bash
# ---------------------------------------------------
# Thumbnailer for Ms Office documents
#
# Procedure :
#   http://bernaerts.dyndns.org/linux/76-gnome/325-gnome-shell-generate-msoffice-thumbnail-nautilus
# Depends on :
#   * unoconv
#   * unzip
#   * mimetype (libfile-mimeinfo-perl package)
#   * pbmmake, pngtopnm, pnmtopng, pnmscalefixed and pnmcomp (netpbm package)
#   * gvfs-copy (gvfs-bin package)
# Parameters :
#   $1 - URI of office file
#   $2 - full path of generated thumbnail
#   $3 - height of thumbnail in pixels
# Revision history :
# 11/11/2014, V1.0 - Creation by N. Bernaerts
# 15/11/2014, V2.0 - Change to URI and GVFS to handle thumbnails on network shares
# 31/07/2015, V2.1 - More robust unoconv command line (thanks to Vaderqk)
# 07/10/2017, V2.2 - Speedup with netpbm tools
# ---------------------------------------------------

# check tools availability
command -v unoconv >/dev/null 2>&1 || exit 1
command -v unzip >/dev/null 2>&1 || exit 1
command -v pbmmake >/dev/null 2>&1 || exit 1
command -v pngtopnm >/dev/null 2>&1 || exit 1
command -v pnmscalefixed >/dev/null 2>&1 || exit 1
command -v pnmcomp >/dev/null 2>&1 || exit 1
command -v pnmtopng >/dev/null 2>&1 || exit 1

# get parameters
SCRIPT_NAME=$(basename $0)
FILE_URI="$1"
FILE_THUMB="$2"
SIZE="$3"

# set working path
PATH_SYSTEM="/usr/local/sbin/${SCRIPT_NAME}.res"
PATH_CACHE="$HOME/.cache/${SCRIPT_NAME}"
[ ! -d "${PATH_CACHE}" ] && mkdir --parent "${PATH_CACHE}"

# get filename extension
FILE_EXT=$(echo "$FILE_URI" | sed 's/^.*\.\(.*\)/\1/')

# generate temporary local filename
TMP_LOCAL=$(mktemp -t "XXXXXXXX.${FILE_EXT}")
TMP_PNM=$(mktemp -t "XXXXXXXX.pnm")

# if file is a remote one
URI_TYPE="${FILE_URI:0:4}"
if [ "${URI_TYPE}" = "file" ]
then
    # convert URI to local path
    FILE_PATH=$(printf '%b' "${FILE_URI//%/\\x}")

    # extract local path
    FILE_LOCAL="${FILE_PATH:7}"
else
    # check gvfs-copy availability
    command -v gvfs-copy >/dev/null 2>&1 || exit 1

    # copy input file to temporary local file
    gvfs-copy "${FILE_URI}" "${TMP_LOCAL}"
    FILE_LOCAL="${TMP_LOCAL}"    
fi

# get the file mime type (application/msword, ...)
MIMETYPE=$(mimetype -b "${FILE_LOCAL}")

# determine icon type according to mime type
case ${MIMETYPE} in

    # text
    "application/msword" | \
    "application/vnd.ms-word" | \
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document" )
    LIBREOFFICE_TYPE="odt"
    ;;

    # spreadsheet
    "application/vnd.ms-excel" | \
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | \
    "application/vnd.openxmlformats-officedocument.spreadsheetml.template" )
    LIBREOFFICE_TYPE="ods"
    ;;

    # presentation
    "application/vnd.ms-powerpoint" | \
    "application/vnd.openxmlformats-officedocument.presentationml.presentation" | \
    "application/vnd.openxmlformats-officedocument.presentationml.template" | \
    "application/vnd.openxmlformats-officedocument.presentationml.slideshow" )
    LIBREOFFICE_TYPE="odp"
    ;;

    * )
    ;;
esac

# temporary libreoffice file
TMP_LIBREOFFICE=$(mktemp -t XXXXXXXX.${LIBREOFFICE_TYPE})

# if needed, create tag and mask of document type
DOC_TAG="${PATH_CACHE}/${LIBREOFFICE_TYPE}.pnm"
DOC_ALPHA="${PATH_CACHE}/${LIBREOFFICE_TYPE}-alpha.pnm"
[ ! -f "${DOC_TAG}" ] && pngtopnm "${PATH_SYSTEM}/msoffice-${LIBREOFFICE_TYPE}.png" > "${DOC_TAG}"
[ ! -f "${DOC_ALPHA}" ] && pngtopnm -alpha "${PATH_SYSTEM}/msoffice-${LIBREOFFICE_TYPE}.png" > "${DOC_ALPHA}"

# convert document to libreoffice equivalent format
unoconv -e PageRange=1-1 -o "${TMP_LIBREOFFICE}" -f "${LIBREOFFICE_TYPE}" "${FILE_LOCAL}"

# extract thumbnail
unzip -p "${TMP_LIBREOFFICE}" "Thumbnails/thumbnail.png" | pngtopnm - > "${TMP_PNM}"

# generate thumbnail
pbmmake -white 188 256 | pnmcomp -xoff 2 -yoff=52 "${TMP_PNM}" - | pnmcomp -align left -valign top -alpha "${DOC_ALPHA}" "${DOC_TAG}" - | pnmscalefixed -xysize ${SIZE} ${SIZE} - | pnmtopng -downscale -compression 0 - > "${FILE_THUMB}"

# remove temporary files
rm -R "${TMP_LOCAL}" "${TMP_PNM}" "${TMP_LIBREOFFICE}"

It is using some icons available from my GitHub repository that need to be stored under /usr/local/sbin/msoffice-thumbnailer-icons :

All these icons are used by the thumbnailer script.

3. Thumbnailer Integration

To allow Nautilus to use the proper thumbnailer for Ms Office documents, you need to declare the previous script as the thumbnailer for all the mime type of these documents (application/msword, ...).

MSDN is providing an exhaustive list of Ms Office documents mime types.

This declaration is done via a msoffice.thumbnailer file placed under /usr/share/thumbnailers. This file follows a specific format, where association between the mime type and the thumbnailer is done.

A complete description of this file format is available from Gnome Developers site on the page Installing a Thumbnailer Program.

As we want to get thumbnails even for file which are accessed thru a network share, we will use the file URI instead of it's plain filename. This will allow the thumbnailer script to handle the files thru GVFS.

/usr/share/thumbnailers/msoffice.thumbnailer
[Thumbnailer Entry]
TryExec=/usr/local/sbin/msoffice-thumbnailer
Exec=/usr/local/sbin/msoffice-thumbnailer %u %o %s
MimeType=application/msword;application/vnd.ms-word;application/vnd.openxmlformats-officedocument.wordprocessingml.document;application/vnd.ms-excel;application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;application/vnd.openxmlformats-officedocument.spreadsheetml.template;application/vnd.ms-powerpoint;application/vnd.openxmlformats-officedocument.presentationml.presentation;application/vnd.openxmlformats-officedocument.presentationml.template;application/vnd.openxmlformats-officedocument.presentationml.slideshow;application/ms-office;

4. Thumbnailer Installation

You can download and install the complete LibreOffice Thumbnailer environment from my GitHub repository :

Terminal
# wget https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/thumbnailer/msoffice/msoffice-thumbnailer-install.sh
# chmod +x msoffice-thumbnailer-install.sh
# ./msoffice-thumbnailer-install.sh

You can now purge your thumbnails to allow them to re-generate and restart nautilus to reload the thumbnailer scripts.

# rm -r $HOME/.cache/thumbnails
# nautilus -q

Please note that if you run Lubuntu, thumbnails should be removed under $HOME/.thumbnails and thumbnailer will work after next reboot.

You can now enjoy to see your Ms Office documents displayed as thumbnails.

As it is using LibreOffice conversion engine, generation may take some time.


Hope it helps.


Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus