29

I have an org file with multiple tables

$ cat ~/foo.org
#+Title: hello world

* section 1

lorem ipsum

#+TBLNAME: first-table
| i      | want | to    |
| export | this | table |

#+TBLNAME: second-table
| this | table | is        |
| not  | as    | important |

This file is updated regularly. I have a perl script that parses the csv file obtained from issuing M-x org-table-export on first-table. I would like to be able to export first-table to csv from the command line so I don't have to do this manually every time. Is this possible?

Brian Fitzpatrick
  • 2,265
  • 1
  • 17
  • 40
  • 2
    In order to get the contents of the table in an Org file you could do `(org-babel-ref-resolve "first-table")` - this will give you the table as a list, and then call `orgtbl-to-csv` on that list. See the documentation of `orgtbl-to-csv` for additional arguments. You could then run Emacs in batch mode to execute your function either using `--eval` or `-f` options. – wvxvw Sep 15 '15 at 20:13

1 Answers1

32

You can look for the table, and use org-table-export to export it. You can put the function in a file, load it, and batch export. Something like this maybe:

setup.el:

(require 'org)

(defun my-tbl-export (name)
  "Search for table named `NAME` and export."
  (interactive "s")
  (outline-show-all)
  (let ((case-fold-search t))
    (if (search-forward-regexp (concat "#\\+NAME: +" name) nil t)
    (progn
      (next-line)
      (org-table-export (format "%s.csv" name) "orgtbl-to-csv")))))

Then with your example file, you can batch export like this:

$ emacs --batch foo.org -l setup.el --eval '(my-tbl-export "first-table")'

My search for the table is a bit crude, but it works.

Gina White
  • 344
  • 2
  • 10
suvayu
  • 1,568
  • 10
  • 17