From Emacs To Microsoft Word (And Beyond, Really)

You’ll notice Emacs inspires words like “magical,” “beautiful,” and “elegant” in descriptions of its interface and functionality. It’s not surprising when you consider how nicely it simplifies everyday work tasks. (Once you have dedicated the time necessary to progress past the dizzying learning curve, of course.)

In my case, I needed an easy way to export my Org Mode documents to the myriad formats and specifications of the writer’s market. Some custom functionality got it done, and I’m finally able to say, through trial and error, that my entire process for composing and submitting fiction (and, prospective publication, of course) is locked in.

Coincidentally, Neal Stephenson mentioned this same problem to Lex Fridman, and they made a clip of it. “The publisher put their foot down, and they want it in Word format now.”

Let’s get into this.

Exporting from Emacs to Word (Or, Wherever)

The theory is simple. I use LibreOffice Writer as a go-between for Emacs and other formats needed in the writer’s marketplace (mostly Microsoft Word or Rich Text Format). This way, you can compose in Emacs without having to reformat your document every time a certain market demands slightly different parameters.

Emacs --> LibreOffice template(s) --> Microsoft Word (.docx)

Using my csm/office-export function, I can have a virtually infinite amount of templates. If a particular publication demands a unique specification, I can generate a new template for it.

Packages used

To fully replicate the functionality in my video on this topic you will need the vertico and orderless completion packages for minibuffer completion magic.

Custom Export Function

Here is the function I use for ODT export. I make no guarantees, of course, but it works well for me. If you want to try it out you can simply replace the dummy values with your own personal template “nice names” and file paths.

(defun csm/office-export ()
  "Export Org file to ODT with a user-selected template using nice names."
  (interactive)
  ;; Ask if we should keep line breaks
  (let* ((preserve-line-breaks (y-or-n-p "Do you want to preserve line breaks? "))
         ;; Ask if we should export just the body (no header, footer, title, or author)
         (body-only (y-or-n-p "Body only?"))
         (templates '(("Template 1" . "~/path/to/template1.ott")
                      ("Template 2" . "~/path/to/template2.ott")
                        ("Template 3" . "~/path/to/template3.ott")))
         (nice-name (completing-read "Choose ODT template: " (mapcar 'car templates)))
         (template-path (cdr (assoc nice-name templates))))

    ;; Use `let` to locally bind export options
    (let ((org-export-preserve-breaks preserve-line-breaks)
          (org-export-with-toc (not body-only))
          (org-export-with-title (not body-only))
          (org-export-with-author (not body-only))
          (org-odt-styles-file template-path))

      ;; Export the current Org buffer to ODT
      (org-odt-export-to-odt))))

Once I run the above function, it will prompt me with a few questions: Do I want to preserve line breaks? Yes or no?  There are some publications that want hard line breaks, so I have this option available. Do I want to export the body only?  Likewise, there are some publications that prefer “blind submissions” without the author’s name present anywhere in the manuscript. The “body only” option works well for this.

Then, the most important part, the function will cycle through my hard-coded templates, and I can select the one I want. That’s it.

Once you have your OpenOffice document (the .odt file), you can open it, make any last-minute tweaks, like adding a word count, etc., and then save it as a Microsoft Word (.docx) file.

If you don’t know how to make your own Open Office template, you can check out my older video on the subject. It’s pretty simple: just do a base export from Org Mode, customize it, and save it as a template (.ott) file.

6 Comments

  1. Hi, nice template.
    Why don’t you export directly to docx format by changing (setq org-odt-preferred-output-format “docx”) ;;default value is nil.
    You could integrate that to your template in case you don’t always export to docx.
    keep on sharing on emacs writing ! 🙂

  2. I prefer to write directly in LaTeX and not use org-mode at all. I then convert from tex to docx via pandoc – all in emacs of course.

Leave a Reply

Your email address will not be published.


*