NLeSC / litstudy

LitStudy: Using the power of Python to automate scientific literature analysis from the comfort of a Jupyter notebook
https://nlesc.github.io/litstudy/
Apache License 2.0
167 stars 50 forks source link

Export `DocumentSet` to bibtex #12

Open stijnh opened 2 years ago

stijnh commented 2 years ago

Currently, there is no way to export results after loading them into memory. On possibility would be to add the ability to export a DocumentSet to a bibtex file.

stephen-pilli commented 2 weeks ago

Hopefully this workaround will help.

import bibtexparser

def export_documentset_to_bibtex(document_set, output_filename="exported_file.bib"):
    """
    Exports entries from a litstudy DocumentSet to a .bib file.

    Parameters:
        document_set (DocumentSet): A litstudy DocumentSet containing documents to export.
        output_filename (str): The name of the output .bib file (default: "exported_file.bib").
    """
    # Convert DocumentSet entries to bibtexparser format
    bibtex_entries = []
    for entry in document_set:
        bibtex_entry = entry.entry
        bibtex_entries.append(bibtex_entry)

    # Create a BibDatabase object and set entries
    bib_database = bibtexparser.bibdatabase.BibDatabase()
    bib_database.entries = bibtex_entries

    # Write entries to the output BibTeX file
    with open(output_filename, "w") as bibfile:
        bibtexparser.dump(bib_database, bibfile)

    print(f"BibTeX file exported successfully as '{output_filename}'")