litstudy icon indicating copy to clipboard operation
litstudy copied to clipboard

Export `DocumentSet` to bibtex

Open stijnh opened this issue 3 years ago • 1 comments

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.

stijnh avatar Mar 03 '22 11:03 stijnh

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}'")

stephen-pilli avatar Nov 11 '24 10:11 stephen-pilli