Try the python version of emacs-org parser orgparse

I can't write emacs-lisp, so I searched for an org-mode parser with python, so I tried it. Try reading the sample org file and converting the read result into org text.

Click here for the official documentation. https://orgparse.readthedocs.io/en/latest/

The following sample is used this time.

$ cat sample.org
* Heading 1
   :PROPERTIES:
   :ID:       48c4a07e-95c1-41ff-a1f0-8e2f6ad88761
   :END:
** TODO [#B] Heading 2
   DEADLINE: <2020-05-09 Sat>
** DONE Heading 2
   CLOSED: [2020-05-09 Sat 22:21]
*** Heading 3
    SCHEDULED: <2020-05-09 Sat>
* Heading 1      :ARCHIVE:

First, install the required packages. There are separate packages for reading and writing.

$ pip install orgparse orger

The sample code for reading and writing is below.

$ cat org-parse.py
from orgparse import load
from orger import inorganic

root = load('sample.org')

print('--- for node in root[1:] ---')
#Execute in order from the node in the upper row
for node in root[1:]:
    print(node.heading)

#Execute in order from the node in the upper hierarchy
print('--- for node in root.children ---')
# for node in root.children
for node in root.children:
    print(node.heading)

#Copy to package for writing
def get_node(node):
    children = [get_node(child) for child in node.children]
    scheduled = None if node.scheduled is None else node.scheduled.start

    return inorganic.node(
        heading=node.heading, #Read title
        todo=node.todo, # TODO,Read DONE etc.
        tags=node.tags, #Tag reading
        scheduled=scheduled, #scheduled read,There is also a deadline for reading
        properties=node.properties, #Individual loading is node.get_property('ID')
        body=node.body, #Reading the contents
        children=children
    )
print('--- write to org file ---')
for node in root.children:
    node = get_node(node) 
    print(node.render())

Execution result

$ python3 org-parse.py
--- for node in root[1:] ---
Heading 1
Heading 2
Heading 2
Heading 3
Heading 1
--- for node in root.children ---
Heading 1
Heading 1
--- write to org file ---
* Heading 1
:PROPERTIES:
:ID: 48c4a07e-95c1-41ff-a1f0-8e2f6ad88761
:END:
** TODO Heading 2
** DONE Heading 2
*** Heading 3
SCHEDULED: <2020-05-09 Sat>
* Heading 1 :ARCHIVE:

I was able to read and write org files easily. With this, it seems that todo aggregation etc. can be written with a python script. As of May 09, 2020, it seems that the inorganic side of the write-only package does not support closed or deadline.

The org write package is still in the tentative stage, and it seems that the API is under consideration. Related issues: https://github.com/karlicoss/orgparse/issues/11

By the way, the related repositories this time are as follows. All are made by the same person.

In particular, the package called orger orgs github, pdf, tiwtter, etc. It seems that the goal is to manage all the information in org. It's like the emacs-org version of evernote. I want to use it someday.

Recommended Posts

Try the python version of emacs-org parser orgparse
[Python] Try pydash of the Python version of lodash
Try the free version of Progate [Python I]
pyenv-change the python version of virtualenv
Change the Python version of Homebrew
About the virtual environment of python version 3.7
the zen of Python
A note about the python version of python virtualenv
Try using the collections module (ChainMap) of python3
Let's use the Python version of the Confluence API module.
Align the version of chromedriver_binary
Towards the retirement of Python2
About the ease of Python
Version upgrade of python Anaconda
Check OpenSSL version of python 2.6
About the features of Python
The Power of Pandas: Python
Try scraping the data of COVID-19 in Tokyo with Python
Put the latest version of Python on linux (Debian) on Chromebook
Try to get the function list of Python> os package
Try to automate the operation of network devices with Python
What is the default TLS version of the python requests module?
[Python] Tuple version of prefecture pull-down
Test the version of the argparse module
The story of Python and the story of NaN
Raise the version of pyenv itself
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
Try the Python LINE Pay SDK
Existence from the viewpoint of Python
Ideone> Python version: 3.5 (as of August 29, 2017)
How to get the Python version
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
Try using the Python Cmd module
About the basics list of Python basics
Learn the basics of Python ① Beginners
The story that the version of python 3.7.7 was not adapted to Heroku
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
[Python] Try to graph from the image of Ring Fit [OCR]
Try transcribing the probability mass function of the binomial distribution in Python
First python ② Try to write code while examining the features of python
How to update the python version of Cloud Shell on GCP
Change the length of Python csv strings
Try using the Wunderlist API in Python
Check the behavior of destructor in Python
[Python3] Understand the basics of Beautiful Soup
Try using the Kraken API in Python
How to check the version of Django
Pass the path of the imported python module
The story of making Python an exe
Learning notes from the beginning of Python 1
Check the existence of the file with python
Try python
[Python] Understand the content of error messages
Try to image the elevation data of the Geographical Survey Institute with Python
[Python3] Rewrite the code object of the function
I didn't know the basics of Python
The result of installing python in Anaconda
[python] Checking the memory consumption of variables
Check the path of the Python imported module