Categories
Uncategorized

Import bibtex into Notion with Python

If you are organizing your literature in a bibtex file, which is most likley if you are using Latex or Citavi for writing and researching for a scientific paper and you are also enjoying the beauty of the note taking application Notion, than you are definitly interested in combining these apps. In the following I will describe you how to import your literature collection in form of a bibtex file into a Notion table.

1. Get the code

You can either clone the github repo on you machine or just create manually a new python file and copy the source code below.

from pybtex.database.input import bibtex
import csv

# open a bibtex file
parser = bibtex.Parser()
# change Lib.bib to your target filename
bibdata = parser.parse_file("Lib.bib")

data_list = [
        ["Title", "Year", "Authors"]
    ]

# loop through the individual references
for bib_id in bibdata.entries:
    b = bibdata.entries[bib_id].fields
    entry = []

    try:
        entry.append(b["title"])
        entry.append(b["year"])

        # deal with multiple authors
        authors = []
        for author in bibdata.entries[bib_id].persons["author"]:
            first_names = ' '.join(author.first_names)
            last_names = ' '.join(author.last_names)
            full_name = first_names + ' ' + last_names
            authors.append(full_name)

        entry.append(', '.join(authors))
        data_list.append(entry)

    # field may not exist for a reference
    except(KeyError):
        print("error")
        continue

# save data_list to a csv file called data_export.csv
with open('data_export.csv', 'w', newline='') as file:
    writer = csv.writer(file, quoting=csv.QUOTE_NONNUMERIC, delimiter=';')
    writer.writerows(data_list)

The python module pybtex version 0.22.2 is needed to run the script. So be sure to install it with the command

pip install pybtex==0.22.2

2. Move your bibtex file to your script location

Rename your bibtex file into Lib.bibtex and move it on the same location as your previous created/cloned python script file.

3. Run the script

By double clicking on the python script or via the standard python command on the terminal, execute the python script. After that you should see a file called data_list.csv with your libary as the content.

python main.py

4. Create a Notion table

Inside of Notion create a new table with the same structure as in the data_list.csv, by default it has three columns named Title, Year and Author.

All entries are text fields right now. Feel free to change for example the year column to integer.

Click on the top bar of your Notion table on the three dots.

Select Merge with CSV and select your create data_list.csv file.

And thats it! After that you should have a filled table with your literature. It is also possible to update it after time with the same procedure.

As you can see, right now are just 3 attributes supported but it is possible to adjust the attributes as you prefer on the python script. For help just leave a commend below.

3 replies on “Import bibtex into Notion with Python”

Hey! This is kind of off topic but I need some advice from an established blog. Is it tough to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about setting up my own but I’m not sure where to start. Do you have any ideas or suggestions? Cheers

Hello there! I know this is somewhat off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having difficulty finding one? Thanks a lot!

Leave a Reply

Your email address will not be published. Required fields are marked *