Howto convert org-mode table to original tabbed format?

Here are the steps to use export the table as tab or comma separated values:

  1. Use the command org-table-export. M-x org-table-export
  2. Enter the filename to save to (or hit enter for the same file).
  3. Select the format (this is where you can set the orgtbl-to-tsv or any other formats).

These are some of the formats that can be used:

  • orgtbl-to-csv
  • orgtbl-to-generic
  • orgtbl-to-html
  • orgtbl-to-latex
  • orgtbl-to-orgtbl
  • orgtbl-to-texinfo
  • orgtbl-to-tsv

  1. Mark the region.
  2. M-x replace-string
  3. |
  4. C-q TAB RET

If you want to tweak it, use replace-regex.


I needed this too and just wrote the following based on org-table-export:

(defun org-table-transform-in-place ()
  "Just like `ORG-TABLE-EXPORT', but instead of exporting to a
  file, replace table with data formatted according to user's
  choice, where the format choices are the same as
  org-table-export."
  (interactive)
  (unless (org-at-table-p) (user-error "No table at point"))
  (org-table-align)
  (let* ((format
      (completing-read "Transform table function: "
               '("orgtbl-to-tsv" "orgtbl-to-csv" "orgtbl-to-latex"
                 "orgtbl-to-html" "orgtbl-to-generic"
                 "orgtbl-to-texinfo" "orgtbl-to-orgtbl"
                 "orgtbl-to-unicode")))
     (curr-point (point)))
    (if (string-match "\\([^ \t\r\n]+\\)\\( +.*\\)?" format)
    (let ((transform (intern (match-string 1 format)))
          (params (and (match-end 2)
               (read (concat "(" (match-string 2 format) ")"))))
          (table (org-table-to-lisp
              (buffer-substring-no-properties
               (org-table-begin) (org-table-end)))))
      (unless (fboundp transform)
        (user-error "No such transformation function %s" transform))
      (save-restriction
        (with-output-to-string
          (delete-region (org-table-begin) (org-table-end))
          (insert (funcall transform table params) "\n")))
      (goto-char curr-point)
      (beginning-of-line)
      (message "Tranformation done."))
      (user-error "Table export format invalid"))))

(define-key org-mode-map (kbd "\C-x |") 'org-table-transform-in-place)

It'd be great if this got added to org-mode proper as I think many would use it.


Try orgtbl-to-tsv for tab-separated values.

There is also orgtbl-to-csv for comma-separated values.

Combining the table with a short code block to do the conversion is convenient. For example:

* Some heading
        
#+name: foo 
| a | b | c |
|---+---+---|
| 1 | 2 | 3 |
| 4 | 5 | 6 |
        

#+name: foo-csv
#+BEGIN_SRC elisp :var x=foo :wrap example
(orgtbl-to-csv x nil)
#+END_SRC

#+RESULTS: foo-csv
#+begin_example
1,2,3
4,5,6
#+end_example

C-c C-c on the code block will produce the results shown. Adding :colnames no as a header argument to the code block will also preserve the header line:

#+name: foo-csv
#+BEGIN_SRC elisp :var x=foo :wrap example :results raw :colnames no
(orgtbl-to-csv x nil)
#+END_SRC

#+RESULTS: foo-csv
#+begin_example
a,b,c
1,2,3
4,5,6
#+end_example

Tags:

Emacs

Org Mode