30

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?

1
  • 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, 2015 at 20:13

1 Answer 1

33
+50

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.