Skip to content

Instantly share code, notes, and snippets.


a6kme/ Secret

Created March 26, 2023 13:16
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 a6kme/0786dc4e2445303e206b44f42aac36fa to your computer and use it in GitHub Desktop.
Save a6kme/0786dc4e2445303e206b44f42aac36fa to your computer and use it in GitHub Desktop.
import os
import base64
import io
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from PyPDF2 import PdfReader, PdfWriter
from pypdfocr.pypdfocr_gs import PyGs
from pypdfocr.pypdfocr_tesseract import PyTesseract
# Set the destination folder and PDF password
destination_folder = "/path/to/destination/folder"
pdf_password = "your-pdf-password"
# Authenticate with the Gmail API
def authenticate():
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = Credentials.from_authorized_user_file('token.pickle')
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', [''])
creds = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
return build('gmail', 'v1', credentials=creds)
# Download and decrypt the PDF attachments
def download_attachments(service, query):
result = service.users().messages().list(userId='me', q=query).execute()
messages = result.get('messages', [])
for message in messages:
msg = service.users().messages().get(userId='me', id=message['id'], format='full').execute()
payload = msg['payload']
filename = None
for part in payload.get("parts"):
filename = part.get("filename")
file_data = part.get("body", {}).get("data")
if filename.endswith(".pdf") and file_data:
file_path = os.path.join(destination_folder, filename)
file_data = base64.urlsafe_b64decode(file_data)
decrypt_pdf(file_data, file_path, pdf_password)
def decrypt_pdf(file_data, file_path, password):
with io.BytesIO(file_data) as input_pdf:
reader = PdfReader(input_pdf, password=password)
writer = PdfWriter()
for page in reader.pages:
with open(file_path, "wb") as output_pdf:
if __name__ == '__main__':
service = authenticate()
query = ' after:2022-03-01 has:attachment'
download_attachments(service, query)
except HttpError as error:
print(f"An error occurred: {error}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment