Package: pyoverleaf
PyOverleafUnofficial Python API to access Overleaf.TasksList projectsDownload project as zipList and download individual files/docsUpload new files/docsDelete files, create foldersPython CLI interface to access project filesMove, rename filesCreate, delete, archive, and rename projectsAccess/update comments, perform live changesAccess/update profile detailsRobust loginGetting startedInstall the project by running the following:pipinstall'pyoverleaf[cli]'Before using the API, make sure you are logged into Overleaf in your default web browser.
Currently, only Google Chrome and Mozilla Firefox are supported:https://github.com/richardpenman/browsercookieTest if everything is working by listing the projects:pyoverleaflsPython APIThe low-level Python API provides a way to access Overleaf projects from Python.
The main entrypoint is the classpyoverleaf.ApiAccessing projectsimportpyoverleafapi=pyoverleaf.Api()api.login_from_browser()# Lists the projectsprojects=api.get_projects()# Download the project as a zipproject_id=projects[0].idapi.download_project(project_id,"project.zip")Managing project filesimportpyoverleafapi=pyoverleaf.Api()api.login_from_browser()# Choose a projectproject_id=projects[0].id# Get project filesroot_folder=api.project_get_files(project_id)# Create new foldernew_folder=api.project_create_folder(project_id,root_folder.id,"new-folder")# Upload new file to the newly created folderfile_bytes=open("test-image.jpg","rb").read()new_file=api.project_upload_file(project_id,new_folder.id,"file-name.jpg",file_bytes)# Delete newly added folder containing the fileapi.project_delete_entity(project_id,new_folder)Higher-level Python IO APIThe higher-level Python IO API allows users to access the project files in a Pythonic way.
The main entrypoint is the classpyoverleaf.ProjectIOHere are some examples on how to use the API:importpyoverleafapi=pyoverleaf.Api()api.login_from_browser()# Choose a projectproject_id=projects[0].id# Get project IO APIio=pyoverleaf.ProjectIO(api,project_id)# Check if a path existsexists=io.exists("path/to/a/file/or/folder")# Create a directoryio.mkdir("path/to/new/directory",parents=True,exist_ok=True)# Listing a directoryforentityinio.listdir("path/to/a/directory"):print(entity.name)# Reading a filewithio.open("path/to/a/file","r")asf:print(f.read())# Creating a new filewithio.open("path/to/a/new/file","w+")asf:f.write("new content")Using the CLIThe CLI provides a way to access Overleaf from the shell.
To get started, runpyoverleaf --helpto list available commands and their arguments.Listing projects and files# Listing projectspyoverleafls# Listing project filespyoverleaflsproject-name# Listing project files in a folderpyoverleaflsproject-name/path/to/filesDownloading existing projectspyoverleafdownload-projectproject-nameoutput.zipCreating and deleting directories# Creating a new directory (including parents)pyoverleafmkdir-pproject-name/path/to/new/directory# Deletingpyoverleafrmproject-name/path/to/new/directoryReading and writing files# Writing to a fileecho"new content"|pyoverleafwriteproject-name/path/to/file.txt# Uploading an imagecatimage.jpg|pyoverleafwriteproject-name/path/to/image.jpg# Reading a filepyoverleafreadproject-name/path/to/file.txt