I want to use a (sub)tree of my org file as an imput to a python babel source block to parse it and to stuff with it (in my case generate a folder structure based on the headlines of that tree).

I know I can reference a table and the output of another code block but I didn't find anything to reference a tree.

Is there a way?
Is there probably a hack to explicitely declare a string and reference that?

Thanks for any tipps.

1 Answer 1


You can use org-link-search to find the subtree and org-element-parse-headline to parse the subtree for headlines.

If you do that in a named Elisp source block you can use the output of that source block as input to another source block.

You can use the full heading text for the link search. But, I usually don't do that. I rather mark the heading by a target, e.g., <<mySubtree>> and search for the target label mySubtree.

There follows an example:

* Main Heading
** <<mySubtree>> Marked Subtree
*** Some subsection
*** Next subsection
* Other heading

#+NAME: mySubtreeSearch
#+BEGIN_SRC emacs-lisp
(let ((hl (save-excursion ;; syntax tree of the headline
        (org-link-search "mySubtree")
        (org-element-parse-headline 'headline))))
  (cdr (org-element-map
    (lambda (el)
      (org-element-property :title el)))))

#+RESULTS: mySubtreeSearch
| Some subsection | Next subsection |

#+BEGIN_SRC emacs-lisp :var tree=mySubtreeSearch
;; Here we just use the variable tree

| Some subsection | Next subsection |
  • fantastic. thank you.
    – sebastian
    Feb 9, 2020 at 22:57

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.