0

I'd like to use send highlighted text from .org documents to the clipboard in the docx format. I've tried the following:

       (defun region-to-clip ()
        "Copies region between point and mark to the clipboard"
        (interactive)
        (shell-command-on-region
         (mark)
         (point)
         "pandoc -f org -t docx | xclip -sel clip &> /dev/null"))

Testing this with the following org text:

****** Heading 1

******* Heading 2

1. Element 1
2. Element 2

gives:

PK###

when pasted into Libre Office. I get the same result using odt as the target output from pandoc, but something more reasonable if I choose markdown. The markdown is ok, but ultimately I'd like to also take advantage of pandoc's reference-doc for formatting docx output.

NickD
  • 27,023
  • 3
  • 23
  • 42
Matt Nolan
  • 121
  • 4
  • Maybe something here can help? https://emacs.stackexchange.com/questions/12121/org-mode-parsing-rich-html-directly-when-pasting – mankoff Feb 14 '23 at 15:08
  • Thanks, intersting. In that post they go from html to org via json. Is that what you were thinking of? Have just tried json as an intermediate step here, but doesn't make any difference. – Matt Nolan Feb 14 '23 at 16:22
  • Works for me as is (although I would rewrite it a bit to use `(interactive "r")` to specify the region). I can paste into a an empty file, save it as `foo.docx` and open the resulting file in LO, where it looks reasonable, If I paste directly into LO, it actually strips out all the formatting and pastes just the text of the region I saved: it has lost all structure. – NickD Feb 14 '23 at 23:08
  • I wasn't suggesting that *specific* step, but maybe cleaning or routing through 3rd parties in general can get you what you want. But if it works for @NickD, then it's probably a version issue? – mankoff Feb 15 '23 at 17:55

1 Answers1

1

I wrote ox-clip (https://melpa.org/#/ox-clip) and it works with your example. It covers windows, Mac and Linux for this. The gist is similar to your approach, but it goes through html. The linux version is similar to your approach, but uses the command (https://github.com/jkitchin/ox-clip/blob/ff117cf3c619eef12eccc0ccbfa3f11adb73ea68/ox-clip.el#L83)

xclip -verbose -i \"%f\" -t text/html -selection clipboard

and goes through an intermediate file that is created.

John Kitchin
  • 11,555
  • 1
  • 19
  • 41
  • Thanks! This is package is great. Is there a granular way I can configure the output? E.g. To specify font sizes / colours for different levels of heading, list items, etc. This was my motivation for going via pandoc to docx but fine if it can be done a different way. – Matt Nolan Feb 14 '23 at 22:38
  • I don't know of anything too granular. If you dig through it, you can find an htmlize version that might let you do that. Otherwise, you are probably stuck with a custom exporter to get the html that you want. – John Kitchin Feb 15 '23 at 01:59
  • If you want it configured, ODT export supports ODT templates. So rather than exporting via `xclip`, put the highlighted section in a temp buffer, export using *Org* to ODT (with your template). – mankoff Feb 15 '23 at 18:05