- To: email@example.com
- Subject: File conversion utility
- From: firstname.lastname@example.org
- Date: Sun, 24 Sep 2006 17:52:37 +1000
- Delivered-To: email@example.com
- List-Help: <mailto:firstname.lastname@example.org?subject=help>
- List-Post: <mailto:email@example.com>
- List-Subscribe: <mailto:firstname.lastname@example.org?subject=subscribe>
- List-Unsubscribe: <mailto:email@example.com?subject=unsubscribe>
- Old-Return-Path: <firstname.lastname@example.org>
- Resent-Date: Sun, 24 Sep 2006 03:52:38 -0400 (EDT)
- Resent-From: email@example.com
- Resent-Message-ID: <M2_6JD.A.NVE.GljFFB@mail>
- Resent-Sender: firstname.lastname@example.org
attached is a re-working of my file conversion utility I posted to the
list back in April 2005. This version is substantially changed and I
have taken on some of the suggestions I recieved. In particular
* Now uses emacs custom facility for managing customizable
* The functionality is now integrated into the view-file function.
Now if you try to view a file which is a *.doc, *.ps *.pdf or
*.ppt, the file will be automatically converted to either plain
text or HTML (for ppt).
* In the case of HTML files, this utility now uses the browse-url
package to render the buffer contents as a web page. So if you
are in dired and hit v for view file and the file is either an
HTML file or a PowerPoint file that will be converted to HTML,
after view-file has put the contents into the buffer, the
default browser configured in the standard browse-url package
will render the contents as a web page.
Note that I have developed this utility under emacs 22, but it should
work under emacs 21 as well. Only very minimal testing has been done
and I expect there are some bugs. Bottom line, this is probably best
considered a work in progress.
To use this utility, put it somewhere in your load path and then add
to your .emacs.
You need to have the external packages for doing the file conversion.
See the commentary at the beginning of the source file. Most of these
utilities are available as part of well appointed Linux distributions.
For Debian users, all the utilities are only an apt-get away!
You will also want to make sure you have an appropriate browser
defined for browse-url. Currently, I'm using w3m, but I have used w3
As usual, feedback, bug reports, patches or general suggestions always
welcome. See the file header for contact address etc.
;; Filename: /home/tcross/projects/emacs-convert/txutils.el
;; Creation Date: Wednesday, 20 September 2006 10:13 PM EST
;; Last Modified: Sunday, 24 September 2006 05:35 PM EST
;; Author: Tim Cross <email@example.com>
;; Description: Convert files from doc, ps, pdf, ppt to a format
;; which can be viewed within emacs (i.e. text or html)
;;; Copyright (C) 2006. Tim Cross <firstname.lastname@example.org>
;;; All Rights Reserved.
;;; This file is not part of GNU Emacs, but the same permissions apply.
;;; GNU Emacs is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2, or (at your option)
;;; any later version.
;;; GNU Emacs is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Emacs; see the file COPYING. If not, write to
;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
;;; The very simple idea behind this basic utility is to make accessing
;;; files in .doc, .pdf, .ps and .ppt more easily accessible without
;;; having to leave emacs or manually convert the file format prior
;;; to being able to view the contents in emacs.
;;; There are packages which will enable calls to external viewers
;;; for files of specific formats, such as xpdf for pdf etc. However,
;;; I wanted to have everything within emacs as this makes integration,
;;; cutting/pasting etc a lot easier, plus as a blind user, most
;;; external utilities are of little use because they don't also include
;;; speech support.
;;; The objective here is to have things setup so that when browsing
;;; a directory with dired, you can just hit 'v' for any file you want to
;;; view and you will be presented with a text or html version without
;;; needing to do any manual conversion - or even careing about what
;;; would need to be done.
;;; You need the following packages (or at least utilities which will
;;; do the same thing). Most of these are fairly standard with many Linux
;;; distros these days.
;;; The wv utilities which contain wvText for converting MS Word docs
;;; The xpdf utilities which include pdftotext for converting PDF to text
;;; The Ghostscript package which contains pstotext for converting PS to text
;;; The ppthtml utility for converting MS Power Point files to html
;;; A configured and working browse-url setup. I use w3m as my browser
;;; The variables used to hold the paths to programs used to convert
;;; various file formats have been defined using the custom package.
;;; These conversion programs are called with two arguments, the input
;;; file and the output file for the converted text. Use the command
;;; M-x customize-group <ret> txutils <ret> to set these values if the
;;; default values are not sufficient.
;;; Pretty straight forward. Place this file somewhere in your load path
;;; and put a (require 'txutils) in your .emacs. You may want to byte
;;; compile this file.
;;; Reporting Bugs
;;; This is the first bit of elisp I've allowed out into the world and
;;; while I am really learning to love both elisp and cl lisp, I'm still
;;; very much a novice. Therefore, there IS bugs and probably some pretty
;;; poor style within this stuff. Feedback, bug reports and suggestions
;;; always welcome. Send e-mail to email@example.com
;;; Emacspeak Users Note - I've not attempted to enhance this code to provide
;;; better spoken or audio icon feedback. When the code matures a bit and
;;; once I get some feedback, I will see if a re-worked version can be
;;; included in emacspeak. In the meantime, feel free to use 'advice' to
;;; improve things.
(defgroup txutils nil
"Customize group for txutils."
(defcustom txutils-msword2text-prog "/usr/bin/wvText"
"Program to convert MS Word .doc files to text."
(defcustom txutils-pdf2text-prog "/usr/bin/pdftotext"
"Program to convert PDF file to text."
(defcustom txutils-ps2text-prog "/usr/bin/pstotext"
"Program to convert PostScript files to text."
(defcustom txutils-ppt2html-prog "/usr/bin/ppthtml"
"Program to convert MS PowerPoint slides to HTML."
(defun txutils-run-command (cmd arg1 arg2 &optional output-buffer)
"Execute shell command with arguments, putting output in buffer."
(if (= 0 (shell-command (format "%s %s %s" cmd arg1 arg2)
(if (= 0 (shell-command (format "%s %s %s" cmd arg1 arg2)
(defun txutils-quote-expand-file-name (file-name)
"Expand file name and quote special chars if required."
(shell-quote-argument (expand-file-name file-name)))
(defun txutils-file-type (file-name)
"Return symbol representing file type."
((string-match "\\.\\(?:DOC\\|doc\\)$" file-name)
((string-match "\\.\\(?:PDF\\|pdf\\)$" file-name)
((string-match "\\.\\(?:PS\\|ps\\)$" file-name)
((string-match "\\.\\(?:PPT\\|ppt\\)$" file-name)
((string-match "\\.\\(?:HTML?\\|html?\\)$" file-name)
(defun txutils-make-temp-name (orig-name type)
"Create a temp file name from original file name."
(let ((name-prefix (file-name-nondirectory orig-name)))
((eq 'ppt type)
(make-temp-file name-prefix nil ".html"))
(t (make-temp-file name-prefix nil ".txt")))))
(defun txutils-do-file-conversion (file-name)
"Based on file extension, convert file to text. Return name of text file."
(interactive "fFile to convert: ")
(let* ((file-type (txutils-file-type file-name))
(output-file (txutils-make-temp-name file-name file-type)))
(message "Performing file conversion for %s." file-name)
((eq 'doc file-type)
(if (txutils-run-command txutils-msword2text-prog
((eq 'pdf file-type)
(if (txutils-run-command txutils-pdf2text-prog
((eq 'ps file-type)
(if (txutils-run-command txutils-ps2text-prog
(concat "-output "
((eq 'ppt file-type)
(if (txutils-run-command txutils-ppt2html-prog
(concat "> "
((eq 'html file-type)
(defadvice view-file (around txutils pre act comp)
"Perform file conversion or call web browser to view contents of file."
(setq file-type (txutils-file-type (ad-get-arg 0)))
(when (and (not (eq 'plain file-type))
(not (eq 'html file-type)))
(setq ad-new-arg (txutils-do-file-conversion (ad-get-arg 0)))
(ad-set-arg 0 ad-new-arg))
(if (eq 'html (txutils-file-type (ad-get-arg 0)))
There are two types of people in IT - those who do not manage what they
understand and those who do not understand what they manage.