{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pdf2bib" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def extract_metadata(file_path):\n", " pdfextractdata = pdf2bib.pdf2bib(file_path)\n", " #st.write(pdfextractdata)\n", " pdfextractdata_metadata = {} if pdfextractdata.get('metadata', {}) is None else pdfextractdata.get('metadata', {})\n", "\n", " return pdfextractdata_metadata" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[pdf2bib]: Trying to extract data to generate the BibTeX entry for the file: boiteau-et-al-2024-relating-molecular-properties-to-the-persistence-of-marine-dissolved-organic-matter-with-liquid.pdf\n", "[pdf2bib]: Calling pdf2doi...\n", "[pdf2doi]: Trying to retrieve a DOI/identifier for the file: boiteau-et-al-2024-relating-molecular-properties-to-the-persistence-of-marine-dissolved-organic-matter-with-liquid.pdf\n", "[pdf2doi]: Method #1: Looking for a valid identifier in the document infos...\n", "[pdf2doi]: Validating the possible DOI 10.1021/acs.est.3c08245 via a query to dx.doi.org...\n", "[pdf2doi]: The DOI 10.1021/acs.est.3c08245 is validated by dx.doi.org.\n", "[pdf2doi]: A valid DOI was found in the document info labelled '/prism:doi'.\n", "[pdf2bib]: pdf2doi found a valid identifier for this paper.\n", "[pdf2bib]: Parsing the info returned by dx.doi.org...\n", "[pdf2bib]: A valid BibTeX entry was generated.\n" ] } ], "source": [ "a = pdf2bib.pdf2bib(\"boiteau-et-al-2024-relating-molecular-properties-to-the-persistence-of-marine-dissolved-organic-matter-with-liquid.pdf\")" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'@article{boiteau2024relating,\\n\\ttitle = {Relating Molecular Properties to the Persistence of Marine Dissolved Organic Matter with Liquid Chromatography–Ultrahigh-Resolution Mass Spectrometry},\\n\\tpublisher = {American Chemical Society (ACS)},\\n\\turl = {http://dx.doi.org/10.1021/acs.est.3c08245},\\n\\tdoi = {10.1021/acs.est.3c08245},\\n\\tjournal = {Environmental Science & Technology},\\n\\tyear = {2024},\\n\\tmonth = {2},\\n\\tauthor = {Rene M. Boiteau and Yuri E. Corilo and William R. Kew and Christian Dewey and Maria Cristina Alvarez Rodriguez and Craig A. Carlson and Tim M. Conway}\\n}'" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.get(\"bibtex\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "import bibtexparser" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "parser = bibtex.Parser()" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "parser = bibtexparser.bparser.BibTexParser(common_strings=True)\n", "bib_database = bibtexparser.loads(a.get(\"bibtex\"), parser=parser)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "def format_author_names(authors_str):\n", " authors = authors_str.split(' and ')\n", " formatted_authors = []\n", " for author in authors:\n", " parts = author.split()\n", " if len(parts) == 2: # Simple case: First Last\n", " last, first = parts[1], parts[0]\n", " formatted_authors.append(f\"{last}, {first[0]}.\")\n", " elif len(parts) > 2: # Handling middle names or initials\n", " last = parts[-1]\n", " initials = ''.join(f\"{part[0]}.\" for part in parts[:-1])\n", " formatted_authors.append(f\"{last}, {initials}\")\n", " if len(formatted_authors) > 1:\n", " formatted_authors_str = ', '.join(formatted_authors[:-1]) + ', & ' + formatted_authors[-1]\n", " else:\n", " formatted_authors_str = formatted_authors[0]\n", " return formatted_authors_str" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "def format_apa(entry):\n", " author = format_author_names(entry.get('author', ''))\n", " year = entry.get('year', '')\n", " title = entry.get('title', '')\n", " journal = entry.get('journal', '')\n", " volume = entry.get('volume', '')\n", " issue = entry.get('issue', '')\n", " pages = entry.get('page', '').replace('-', '–') # En dash for page range\n", " doi = entry.get('doi', '')\n", " \n", " # Constructing the citation\n", " apa_citation = f\"{author} {title}. {journal} {volume}, {pages} ({year}). https://doi.org/{doi}\"\n", " return apa_citation" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Boiteau, R.M., Corilo, Y.E., Kew, W.R., Dewey, C., Rodriguez, M.C.A., Carlson, C.A., & Conway, T.M. Relating Molecular Properties to the Persistence of Marine Dissolved Organic Matter with Liquid Chromatography–Ultrahigh-Resolution Mass Spectrometry. Environmental Science & Technology , (2024). https://doi.org/10.1021/acs.est.3c08245'" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format_apa(bib_database.entries[0])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.2" } }, "nbformat": 4, "nbformat_minor": 2 }