Skip to content

Instantly share code, notes, and snippets.

@IgnacioHeredia
Created January 2, 2023 11:55
Show Gist options
  • Save IgnacioHeredia/86ac3df4698349341dc721926f038eb3 to your computer and use it in GitHub Desktop.
Save IgnacioHeredia/86ac3df4698349341dc721926f038eb3 to your computer and use it in GitHub Desktop.
Rename arxiv papers with their original title. You have to put and run this script in the same folder as the pdfs to be renamed.
#!/usr/bin/env python3
"""
Rename arxiv papers with their original title. You have to put and run this script in the same folder as the pdfs to be renamed.
Author: Ignacio Heredia
Date: October 2017
"""
import os
import requests
import xmltodict
import re
homedir = os.path.dirname(os.path.realpath(__file__))
pattern = re.compile('^[0-9]{4}.[0-9]{5}$') #pattern of arxiv titles eg. '1708.07120'
for f in sorted(os.listdir(homedir)):
if f.endswith(".pdf"):
fname = f.split('.pdf')[0]
fname = fname[:10] # forget version information
if pattern.match(fname) is None:
print('{} is not an arxiv paper number format'.format(fname))
continue
try:
url = 'http://export.arxiv.org/api/query?search_query={}'.format(fname)
r = requests.get(url)
d = xmltodict.parse(r.content)
if type(d['feed']['entry']) is list:
title = d['feed']['entry'][0]['title']
else:
title = d['feed']['entry']['title']
os.rename(os.path.join(homedir, f), os.path.join(homedir, title + '.pdf'))
print('{} --> {}'.format(fname, title))
except:
print('{} is not found in arxiv'.format(fname))
pass
## Using the title metadata (problem: some PDFs don't have set correctly the metadata)
#from PyPDF2 import PdfFileWriter, PdfFileReader
#def get_pdf_title(pdf_file_path):
# with open(pdf_file_path) as f:
# pdf_reader = PdfFileReader(f)
# return pdf_reader.getDocumentInfo().title
#for f in sorted(os.listdir(homedir)):
# if f.endswith(".pdf"):
# try:
# pdf_path = os.path.join(homedir, f)
# print '{} --> {}'.format(f, get_pdf_title(pdf_path))
# except:
# pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment