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