This code helps to convert a directory with Python files into an org-mode file with code blocks. Change the value of :dir if you want another path. Do ~C-c C-c~ or ~C-c C-v C-e~ (~org-babel-execute-maybe~) on the next block. It will (hopefully) generate a file named *code-blocks.org* which you could (possibly) use to load into the Library of Babel by doing ~M-:(org-babel-lob-ingest "path/to/file.org")~ #+HEADER: :results none #+BEGIN_SRC bash :dir ./ #!/bin/bash # * Set a general configuration cat << EOF > code-blocks.org; All my functions are in this file. If you want to create individual files, run ~M-x org-babel-tangle~. Make sure that there is a directory for each heading. ,* License EOF cat LICENSE.txt >> code-blocks.org cat <> code-blocks.org ,* Configuration :noexport:ARCHIVE: When exporting, print table of contents, author and date ,#+OPTIONS: toc:t author:t date:t When opening this file, visually indent lines according to headers ,#+STARTUP: indent hidestars By default, set Python blocks to run with Python 3, do not evaluate when exporting (quick) and work in a dedicated session. ,#+PROPERTY: header-args:python ":python python3 :eval no-export :session EOF printf "* Code to generate this file\n" >> code-blocks.org; cat dir-to-org-babel.org >> code-blocks.org; printf "\n" >> code-blocks.org; # * Loop over the contents of the current # directory (having two nested while IFS... seemed # too much, but may be a more efficient way) for dir in *; do # ** Make sure that we will look into directories if [[ -d "$dir" ]] && [[ ! "$dir" =~ "Moose" ]]; then # *** Make the name of the sub-dir a heading printf "* $dir\n"; # ** Loop over the files of the current sub-dir # https://stackoverflow.com/a/301059 this # is safe (requires that ~find~ can use # ~-print0~) while IFS= read -r -d '' file; do # *** Print file only if it has code # **** Get number of lines with awk numl=$(awk 'END{print NR}') if [[ "$numl" -gt 0 ]]; then # *** Print the file names (in quotation # format; with spaces like \ ) printf '** %q\n' "${file##./}" | sed "s-$dir/--"; # *** Print a name for the block, # substitute slashes with dashes, # remove ./ # **** Create prototype for name a=py-$(printf "$file" | sed 's_\./__g; s_/_-_g') # **** Print name header printf "#+NAME: $a\n" # *** Print a caption for the block # (use same prototype as the name) printf "#+CAPTION: $a\n"; # *** Print start of the block printf "#+BEGIN_SRC python :tangle $file\n"; cat "$file"; # *** Print end of block printf "#+END_SRC\n\n"; else printf "\n"; fi; done < <(# **** List all files that end with .py find ./"$dir" -mindepth 1\ -type f -iname '*.py' -print0) fi; # ** Export everything that is printed to a file done >> code-blocks.org #+END_SRC