###前提
Pythonの勉強を独学でやっているものです。
勉強を始めて日が浅いので、訳の分からないことを言っているかもしれませんが、温かい目で見てください・・・
実現したいこと
google APIにてGmailの情報をPandasを使って、Excelに書き込みしたいです
具体的には
・メールの送信者
・受信日
・件名
・本文
上記の要素を受信ボックスのすべてのメール分取得したいと思っています。
Gmailの取得と要素の取得についてはできているのですが、どうやってPndasでデータを格納するのかをご教授いただければと思います。
抽象的な質問で申し訳ありませんが
また可能であれば、皆様がPandsについての理解を深めた際に用いたサイトなどがあれば教えていただきたいです。
下記に現在書いているコードを載せておきます。
(ほぼサイトのコピペですが・・・)
Python
1 2from google.oauth2.credentials import Credentials 3from googleapiclient.discovery import build 4import json 5import base64 6import pandas as pd 7 8 9def get_header(headers, name): 10 for h in headers: 11 if h['name'].lower() == name: 12 return h['value'] 13 14 15def base64_decode(data): 16 return base64.urlsafe_b64decode(data).decode() 17 18 19def base64_decode_file(data): 20 return base64.urlsafe_b64decode(data.encode('UTF-8')) 21 22 23def get_body(body): 24 if body['size'] > 0: 25 return base64_decode(body['data']) 26 27 28def get_parts_body(body): 29 if (body['size'] > 0 30 and 'data' in body.keys() 31 and 'mimeType' in body.keys() 32 and body['mimeType'] == 'text/plain'): 33 return base64_decode(body['data']) 34 35 36def get_parts(parts): 37 for part in parts: 38 if part['mimeType'] == 'text/plain': 39 b = base64_decode(part['body']['data']) 40 if b is not None: 41 return b 42 if 'body' in part.keys(): 43 b = get_parts_body(part['body']) 44 if b is not None: 45 return b 46 if 'parts' in part.keys(): 47 b = get_parts(part['parts']) 48 if b is not None: 49 return b 50 51 52def get_attachment_id(parts): 53 for part in parts: 54 if part['mimeType'] == 'image/png': 55 return part['body']['attachmentId'], 'png' 56 return None, None 57 58def excel_output(output): 59 df = pd.DataFrame(output) 60 writer = pd.ExcelWriter('sample.xlsx',options={'strings_to_urls': False}) 61 df.to_excel(writer, sheet_name="Sheet1")#Writerを通して書き込み 62 writer.close() 63 df 64 65def main(): 66 scopes = ['https://mail.google.com/'] 67 creds = Credentials.from_authorized_user_file('token.json', scopes) 68 service = build('gmail', 'v1', credentials=creds) 69 output = [] 70 71 messages = service.users().messages().list( 72 userId='me', 73 ).execute().get('messages') 74 75 for message in messages: 76 print('=' * 10) 77 m_data = service.users().messages().get( 78 userId='me', 79 id=message['id'] 80 ).execute() 81 82 # ヘッダー情報 83 headers = m_data['payload']['headers'] 84 85 # 日付 86 message_date = get_header(headers, 'date') 87 print(f'日付: {message_date}') 88 89 # 差出人 90 from_date = get_header(headers, 'from') 91 print(f'差出人: {from_date}') 92 93 # 宛先 94 to_date = get_header(headers, 'to') 95 print(f'宛先: {to_date}') 96 97 # 件名 98 sub_date = get_header(headers, 'subject') 99 print(f'件名: {sub_date}') 100 101 body = m_data['payload']['body'] 102 body_data = get_body(body) 103 104 parts_data = None 105 if 'parts' in m_data['payload'].keys(): 106 parts = m_data['payload']['parts'] 107 parts_data = get_parts(parts) 108 # 添付ファイル 109 attachment_id, extension = get_attachment_id(parts) 110 if attachment_id is not None: 111 res = service.users().messages().attachments().get( 112 userId='me', 113 messageId=message['id'], 114 id=attachment_id 115 ).execute() 116 f_data = base64_decode_file(res['data']) 117 118 with open(f'download.{extension}', 'wb') as f: 119 f.write(f_data) 120 121 body_result = body_data if body_data is not None else parts_data 122 print(f'本文: {body_result}') 123 124 output = [from_date,sub_date,parts_data,message_date] #「Output」の配列に格納 125 print(output) 126 127if __name__ == '__main__': 128 main()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/10/12 13:39
2022/10/12 14:06