This mini-guide explains how to connect Google Drive to Google Colab and access your PDF file using Python.
1. Upload Your PDF File to Google Drive
Upload the PDF file you want to use to your Google Drive.
2. Mount Google Drive
Run the following code in your Colab notebook:
from google.colab import drive
drive.mount("/content/drive")
Follow the prompts to authorize access to your Google Drive.
Once mounted, your file will be accessible through the /content/drive directory.
3. Specify the File Path
Define the path to your PDF file:
file_path = "/content/drive/MyDrive/data.pdf"
Replace data.pdf with your actual filename.
If the PDF file is in a subfolder, include the subfolder name, for example Projects, in the path:
file_path = "/content/drive/MyDrive//Projects/data.pdf"
To check whether the file exists at file_path in the Google Colab environment, run:
import os
print(os.path.exists(file_path))
Complete Example
from google.colab import drive
drive.mount("/content/drive")
file_path = "/content/drive/MyDrive/data.pdf"
import os
print(os.path.exists(file_path))
Open the notebook
Open the Google Colab notebook for this PDF mini-guide and run the code as you follow along.
This mini-guide was originally published on PDF Python Hub, where you can find more in-depth Python resources for working with PDFs.
What's Next? Install PyMuPDF in Google Colab
Learn how to install PyMuPDF so you can start working with PDF documents in Google Colab.
Top comments (0)