I would like to download 100+ email from gmail as pdf. It would be too long to manually download all of them via the print option in gmail.
This python script retrieves the emails in the chosen label. How can I convert this email into a pdf.
# source = https://developers.google.com/gmail/api/quickstart/python?authuser=2
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
def main():
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('gmail', 'v1', credentials=creds)
# Call the Gmail API
response= service.users().messages().list(userId="me", labelIds="Label_53", q=None, pageToken=None, maxResults=None, includeSpamTrash=None).execute()
all_message_in_label = []
if 'messages' in response:
all_message_in_label.extend(response['messages'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list(userId="me", labelIds="Label_53", q=None, pageToken=page_token, maxResults=None, includeSpamTrash=None).execute()
all_message_in_label.extend(response['messages'])
if not all_message_in_label:
print('No email LM found.')
else:
# get message from Id listed in all_message_in_label
for emails in all_message_in_label:
message= service.users().messages().get(userId="me", id=emails["id"], format="raw", metadataHeaders=None).execute()
if __name__ == '__main__':
main()
I did a little bit of digging about your problem and I found a few links that could be of use:
On converting your messages to .eml
format this link.
On converting from .eml
to .pdf
these links:
eml2pdf is a python github project which converts eml
files to pdf
but Im not sure if it is working or not. You could check it out to see if it works.
eml-to-pdf is another github project which seems inferior yet working. its written in javascript.
and there is pyPdf which you can use to generate pdf
files. though with this you might need to convert the e-mails and format them yourself.
for more information on message objects formatting you can refer to gmail api python docs get method.
And here is a blog post which does what you are looking for using a different approach though I am not entirely sure if it still works.
I hope it helps. good luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With