Skip to content

Instantly share code, notes, and snippets.

@dnywh
Last active June 25, 2023 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnywh/fd3fce09290b194e003c6870f153fd98 to your computer and use it in GitHub Desktop.
Save dnywh/fd3fce09290b194e003c6870f153fd98 to your computer and use it in GitHub Desktop.
Formats Kobo’s annotation text files into Markdown for use in Notion.
# Designed for Kobo highlights and notes.
# Step 1: Copy Kobo annotations TXT file from the Kobo to your Mac
# Step 2: cd into this directory
# Step 3: Rename the TXT file to `input.txt`
# Step 4: Run `python3 parser.py`
# Step 5: See `output.txt` for the results
def parse_txt_file(input_file, output_file):
with open(input_file, "r") as file:
lines = file.readlines()
with open(output_file, "w") as file:
for i, line in enumerate(lines):
line = line.strip() # Remove leading and trailing whitespace
# Ignore the first line
if i == 0:
continue
# Skip empty lines
if not line:
continue
if line.startswith("Note:"):
line = line.replace("Note: ", "")
line = line.replace("[]", "[ ]") # Convert [] to [ ]
file.write(line + "\n")
file.write(
"\n"
) # Add a new blank line after each line starting with "Note:"
else:
line = line.replace("Note: ", "")
file.write("> " + line + "\n")
file.write("\n") # Add a new blank line after each affected line
print(f"Parsing completed. Result saved to '{output_file}'.")
# Usage
input_file_path = "input.txt" # Optional: Replace with the path to your input file
output_file_path = "output.txt" # Optional: Replace with the desired path for the output file
parse_txt_file(input_file_path, output_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment