Ubuntu – Script to view office documents in Microsoft Office Online

clipboarddropboxpdf

I have to convert a lot of Microsoft Word documents to PDF for my work. It is very important to me to render the formatting as accurately as possible. I successfully installed Office 2010 on Xubuntu 14.04, but there is an issue where I can’t open a document. I can only open a new blank document. So, I have given up there. I then proceeded to search for the most accurate rendering tool for Word to PDF. I looked into online tools such as Zamzar as well as other options. (Unfortunately LibreOffice changes the formatting too much.)

After a few tests, the best rendering I could find was this:
https://view.officeapps.live.com/op/view.aspx?src=

Then you append the URL of the Word document:
https://dl.dropboxusercontent.com/u/4992179/My-Document.docx

Final product:
https://view.officeapps.live.com/op/view.aspx?src=https://dl.dropboxusercontent.com/u/4992179/My-Document.docx

From here I would print to PDF.

As you can see from my link, I would use the Dropbox ‘Public’ folder to be able to get a URL for my document. To get the URL in the Public folder you right click on the file and select ‘Copy public link.’

I would like to streamline this process as much as possible because I have to do it a lot. I am trying to find the best way to do so. I would like to create a script that runs the following:
chromium-browser https://view.officeapps.live.com/op/view.aspx?src=Variable-Representing-Current-Content-Of-Clipboard

The current content of the clipboard when I click on the script will be the Dropbox link to the Word document file.

(I am unable to make a bookmark to the https://view.officeapps.live.com/op/view.aspx?src= section of the URL and then just go to the address bar and paste the Dropbox link after because the officeapps URL automatically forwards to another.)

If anyone has any other ideas, I am certainly open to them and appreciative.

Much thanks in advance.

Best Answer

Nautilus script solution

It wouldn't be too difficult to grab the clipboard contents and work with them, but I can do you one better. The following Nautilus script will open any supported document via Microsoft Office Online. It does so by first copying the document to your public Dropbox folder (if it wasn't there before) and subsequently passing the public URL to the web service:

#!/bin/bash

# Name:         Open in Microsoft Office Online
# Author:       (c) 2015 Glutanimate <https://github.com/Glutanimate/>
# Dependencies: dropbox, a web browser (e.g. firefox, chromium...)
# Installation: https://askubuntu.com/q/574252/81372
#
# License:      GNU GPLv3 (http://www.gnu.de/documents/gpl-3.0.en.html)
# Usage:        open_in_microsoft_office_online <file>

# Settings

DbPath="$HOME/Dropbox"
CopyToDb="yes"  # whether to copy file to public dropbox folder
                # in case it's not there already (no/yes)

# Variables

GuiIcon="dropbox"
GuiTitle="Open in Microsoft Office Online"
MsOfficeUrl="https://view.officeapps.live.com/op/view.aspx?src="

File="$1"
Filename="${File##*/}"

# Functions

gui_notify(){
  ## generic notification function
  notify-send -i "$GuiIcon" "$GuiTitle" "$1"
  echo "$1"
}

# Checks

## check if file selected
if [[ ! -f "$File" ]]; then
  gui_notify "Error: No file selected."
  exit 1
fi

## check if Dropbox running
if ! pgrep dropbox > /dev/null 2>&1; then
  gui_notify "Error: Dropbox isn't running."
  exit 1
fi

## check if Dropbox folder set correctly
if [[ ! -d "$DbPath" ]]; then
  gui_notify "Error: Can't find dropbox folder. Please set DbPath in script."
  exit 1
fi

# Main

## get public URL
DbPubUrl="$(dropbox puburl "$File")"

## optional: copy file to public dropbox folder if it isn't there
if [[ "$CopyToDb" = "yes" && "$DbPubUrl" = "Couldn't get public url: Unknown Error" ]]; then
  ## create public Dropbox folder if it doesn't exist
  [[ ! -d "$DbPath/Public" ]] && mkdir "$DbPath/Public"
  ## copy file to public folder, don't overwrite any existing file
  cp -n "$File" "$DbPath/Public/"
  ## wait for sync to complete
  SyncCounter="0"
  while dropbox filestatus "$DbPath/Public/$Filename" | grep syncing; do
    [[ "SyncCounter" = "0" ]] && gui_notify "Syncing file..."
    sleep 5
    ## wait a maximum of 10 minutes for sync to complete
    if [[ "$SyncCounter" -gt "120" ]]; then
      gui_notify "Error: Sync timeout. Exiting."
      exit 1
      break
    fi
    ((SyncCounter++))
  done
  ## get public URL
  DbPubUrl="$(dropbox puburl "$DbPath/Public/$Filename")"
fi

## check if public URL exists and open in Microsoft Office Online
if [[ "$DbPubUrl" != "Couldn't get public url: Unknown Error" ]]; then
  xdg-open "${MsOfficeUrl}${DbPubUrl}" > /dev/null 2>&1 &
  gui_notify "Opening document in Microsoft Office Online..."
else
  gui_notify "Error: Can't generate public Dropbox link from File."
fi

Configuration

There are two important settings that govern how the script operates:

  • DbPath sets the path to your Dropbox folder. This is ~/Dropbox by default. Please make sure to chage the setting if you've moved your DB folder.
  • CopyToDb controls whether or not to copy files to the Public folder if they're located somewhere else in the file system.

    This option is on by default (yes). If you disable it (no) the script will only handle files in your public Dropbox folder.

Installation instructions

As this is a Nautilus script, you can use the following generic instructions to install it on your system:

How can I install a Nautilus script?


Hope this is what you were looking for!