Showing posts with label emacs. Show all posts
Showing posts with label emacs. Show all posts

Monday, 30 January 2012

Org-mode and Microsoft Word

One of my less enjoyable tasks is to produce a weekly status report following a particular look. This then gets emailed off and compiled with others. Unsurprisingly the format has to be Microsoft Word and no one ever gets the use of format styles correct.

Editing the pro-forma always has me cursing: delete one character too many and fonts change, bullets appear or disappear, all the usual joys of Word.

Emacs, and more specifically org-mode to the rescue. Org has had the ability to export in several formats (typically LaTeX and HTML) for quite some time but now boasts support for OpenDocument format too.

So my status reports are now written using Emacs with a line setting org-export-odt-styles-file to point at a hand handcrafted OpenDocument template file. That sorts out the presentation aspects of the final document. C-c C-e o exports my text to OpenDocument then open that in LibreOffice and save as Microsoft Word format. The conversion from ODT to docx format can be automated: soffice --headless --convert-to docx myfilename.

I wouldn't say that I actually enjoy writing my status reports now, but at least it's considerably less painful, I can just get on with writing information and not with fighting the tools.

Tuesday, 22 November 2011

The power of scripting

Interesting problem last night: I wanted to find out how far it was
between a bunch of places. I had the location post codes buried away
in a stack of form letters in pdf form. So convert those to text,
open in emacs and extract the post codes. I couldn't get multi-line
replace-regexp to work, re-builder successfully highlighted the
line but replace-regexp failed to match the same regex. Oh well, I
just want to hack this out so keyboard macros did the job.

A quick google later and I came up with the static maps API
so threw the post codes at that to get back a JSON object with all the
distances. Then using json.el in ielm, convert that to a lisp
structure, pick that apart to extract the relevant fields and run my
calculations over that, plotting the results using R in org-mode.

We have some fantastic tools at our disposal: this is a fun game.

So, back to the multi-line regex that fails. I've got an input file in
the form:

a
1
b

a
2
b

and want to match the paragraph a..b Using re-builder I can construct
a regex that matches:

"a\\(.*
?\\)*?*b"

But using that regex in replace-regex results in no matches.
Digging in to this it turns out that, because I'm using replace-regexp
interactively the escaping rules are slightly different: I only need a
single backslash before the brackets.

When is a solution good enough? The keyboard macro approach did the
job quickly and easily. I then spent longer trying to debug the
multi-line regexp than it would have taken me to solve the original
problem by hand. But I learnt something in the process so surely that
is time well invested. Besides it was fun!