Jan 5, 2012

Pygmentize files and paste them into Microsoft Word in OS X

Writing some documents using Microsoft Word and I wanted to be able to drop in syntax highlighted versions of the source code. Pygmentize will generate RTF format, which is great for pasting in to Word, but you have to get it there. The best way I've found is using the OS X terminal (I like using iTerm2). I then use a feature of the command line open command, which makes it read from stdin and open the output in TextEdit. This text can then be cut and pasted directly in to Word, preserving all the formatting.

Run the command:

pygmentize -f rtf <file to highlight> | open -f

Then in the TextEdit window, press


c to select all and copy it. Switch to Word and hit

v to paste.

There are comments.

Comments !