Google Drive is a popular cloud storage platform that allows users to share files and folders easily. However, downloading files from Google Drive programmatically can be a bit tricky.
In this blog post, we'll walk you through the process of downloading PDF files from publicly accessible Google Drive links using Python.
Prerequisites
Before we begin, make sure you have the following prerequisites:
Python installed on your computer.
A list of publicly accessible Google Drive links to PDF files that you want to download.
Using the gdown Library
We'll use the gdown library, a Python library that simplifies downloading files from Google Drive by their sharing links. You can install it using pip:
pip install gdown
Creating a Python Script
Here's a step-by-step guide to creating a Python script that downloads PDF files from Google Drive links:
1. Import Libraries
python
Copy code
import gdown
import os
2. Define the List of Google Drive Links
Create a list of Google Drive links to the PDF files you want to download. For example:
'https://drive.google.com/uc?id=FILE_ID_1',
'https://drive.google.com/uc?id=FILE_ID_2',
# Add more links as needed
]
Replace FILE_ID_1, FILE_ID_2, etc., with the actual file IDs from your Google Drive links.
3. Specify the Destination Directory
Specify the directory where you want to save the downloaded PDF files:
DESTINATION_DIR = 'downloaded_files'
This code will create a directory named 'downloaded_files' in your current working directory if it doesn't already exist.
4. Create a Function to Download Files
Define a function that downloads a file from a Google Drive link and saves it to the destination directory:
file_id = file_link.split("=")[-1]
destination_path = os.path.join(destination_dir, f"{file_id}.pdf")
gdown.download(file_link, destination_path, quiet=False)
5. Loop Through the Links and Download Files
Now, loop through the list of Google Drive links and call the download_file_from_drive function for each link:
download_file_from_drive(pdf_link, DESTINATION_DIR)
6. Run the Script
Save your script as a .py file and run it. It will download all the PDF files from the specified links into the 'downloaded_files' directory.
python download_google_drive_pdfs.py
Solution 1: File with ID Name
import gdown
import os
# List of Google Drive PDF file links
pdf_links = [
'https://drive.google.com/uc?id=FILE_ID_1',
'https://drive.google.com/uc?id=FILE_ID_2',
# Add more links as needed
]
# Specify the destination directory for downloaded files
DESTINATION_DIR = 'downloaded_files'
# Create the destination directory if it doesn't exist
os.makedirs(DESTINATION_DIR, exist_ok=True)
# Function to download a file from a Google Drive link
def download_file_from_drive(file_link, destination_dir):
file_id = file_link.split("=")[-1]
destination_path = os.path.join(destination_dir, f"{file_id}.pdf")
gdown.download(file_link, destination_path, quiet=False)
# Loop through the PDF links and download each file
for pdf_link in pdf_links:
download_file_from_drive(pdf_link, DESTINATION_DIR)
print("Download complete.")
Solution 2: File with Defined Name
import gdown
import os
# List of Google Drive PDF file links
pdf_links = [
'https://drive.google.com/uc?id=FILE_ID_1',
'https://drive.google.com/uc?id=FILE_ID_2',
# Add more links as needed
]
# List of corresponding names for each PDF file
pdf_names = [
'Document 1 Name.pdf',
'Document 2 Name.pdf',
# Add more names in the same order as links
]
# Specify the destination directory for downloaded files
DESTINATION_DIR = 'downloaded_files'
# Create the destination directory if it doesn't exist
os.makedirs(DESTINATION_DIR, exist_ok=True)
# Function to download a file from a Google Drive link
def download_file_from_drive(file_link, file_name, destination_dir):
destination_path = os.path.join(destination_dir, file_name)
gdown.download(file_link, destination_path, quiet=False)
# Loop through the PDF links and download each file with the corresponding name
for pdf_link, pdf_name in zip(pdf_links, pdf_names):
download_file_from_drive(pdf_link, pdf_name, DESTINATION_DIR)
print("Download complete.")
Key: Download PDF from Google Drive, Python script to download Google Drive PDF, Automate PDF downloads with Python, Gdown library for Google Drive, Publicly accessible Google Drive links, PDF file download using Python, Google Drive file ID, PDF file management with Python, Google Drive automation, Batch PDF downloading, Google Drive sharing links, PDF download script, Python file management, Web scraping PDF files, Google Drive integration with Python.