Skip to content

Instantly share code, notes, and snippets.

@tanyuan
Created April 1, 2016 12:45
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 tanyuan/132d1849ac1486b8610626843334d60a to your computer and use it in GitHub Desktop.
Save tanyuan/132d1849ac1486b8610626843334d60a to your computer and use it in GitHub Desktop.
Rename academic papers to author-year-title.pdf.
import argparse
import bibtexparser
if __name__ == '__main__' :
parser = argparse.ArgumentParser(description='Extract bib file to author, year, and title.')
parser.add_argument('input', help='Input file (.bib)')
args = parser.parse_args()
filename = args.input
with open(filename) as bibtex_file:
bibtex_str = bibtex_file.read()
bib_database = bibtexparser.loads(bibtex_str)
authors = bib_database.entries[0]["author"]
year = bib_database.entries[0]["year"]
title = bib_database.entries[0]["title"]
authors_list = authors.split(" and ")
author = authors_list[0]
print(author, "-", year, "-", title)
#!/bin/bash
# Rename academic paper PDF to author-year-title.pdf
# Example:
# INPUT: example.pdf
# OUTPUT: Tanyuan - 2016 - Human-Computer Interaction.pdf
# Dependencies:
# cb2bib
# python
# bibtexparser
usage(){
echo "Usage: $0 PDF"
exit 1
}
[[ $# -eq 0 ]] && usage
OLD_PDF=$1
TMP=/tmp/rename-paper.bib
rm -f $TMP
# Get bib information and save to $TMP
cb2bib --doc2bib $OLD_PDF $TMP --sloppy
# Extract from bib
NEW_NAME=`python extract-bib.py $TMP`
echo "author - year - title"
echo "+++++++++++++++++++++"
echo $NEW_NAME
# Rename original PDF
mv $OLD_PDF "$NEW_NAME".pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment