Last active
January 26, 2023 02:25
-
-
Save honux77/ad68a9ee1680dc47f4d7b98583845d7e to your computer and use it in GitHub Desktop.
Google spreadsheet and gmail sample (using python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# 구글 스프레드 시트 제어하기\n", | |
"\n", | |
"1. google developer console에 가서 프로젝트 생성 및 키 생성\n", | |
"2. 실제 사용할 스프레드 시트를 하나 만들어 줍니다.\n", | |
"\n", | |
"* 해당 스프레드시트에 json 파일의 이메일 주소에 쓰기 권한을 추가합니다.\n", | |
"\n", | |
"3. 필요 패키지를 설치합니다.\n", | |
"\n", | |
"```\n", | |
"pip install gspread oauth2client\n", | |
"```\n", | |
"\n", | |
"4. 간단 테스트 코드 실행" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'정호영'" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import gspread\n", | |
"from oauth2client.service_account import ServiceAccountCredentials\n", | |
"\n", | |
"# use creds to create a client to interact with the Google Drive API\n", | |
"scope = ['https://spreadsheets.google.com/feeds']\n", | |
"credential = ServiceAccountCredentials.from_json_keyfile_name(\n", | |
" 'google-id.json', scope)\n", | |
"\n", | |
"client = gspread.authorize(credential)\n", | |
"\n", | |
"# Find a workbook by name and open the first sheet\n", | |
"# Make sure you use the right name here.\n", | |
"sheet = client.open(\"honux-test0530\").sheet1\n", | |
"\n", | |
"'''\n", | |
"Extract and print all of the values\n", | |
"list_of_hashes = sheet.get_all_records()\n", | |
"print(list_of_hashes)\n", | |
"sheet.col_values(2)\n", | |
"sheet.update_cell(3,3, \"Python direct update\")\n", | |
"row = [\"100\", 'some', 'abc']\n", | |
"sheet.insert_row(row, 6)\n", | |
"'''\n", | |
"sheet.cell(2, 2).value\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# 지메일 smtp를 이용한 메일 발송 예제\n", | |
"- gmail 패스워드는 파일로 저장했습니다." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Successfully sent email at Tue May 30 13:22:10 2017.\n" | |
] | |
} | |
], | |
"source": [ | |
"import smtplib, pickle, time, os\n", | |
"from email import encoders\n", | |
"from email.mime.multipart import MIMEMultipart\n", | |
"from email.mime.text import MIMEText\n", | |
"from email.mime.base import MIMEBase\n", | |
"\n", | |
"\n", | |
"sender = '[email protected]'\n", | |
"receiver = ['[email protected]', '[email protected]']\n", | |
"\n", | |
"raw = \"\"\"\\\n", | |
"<h1>코드스쿼드 학습메일 at {}</h1>\n", | |
"모두 행복한 하루 보내세요.\n", | |
"<img src = 'http://public.codesquad.kr/python-basic/img/img_white.png/>\n", | |
"\"\"\"\n", | |
"body = raw.format(time.asctime())\n", | |
"\n", | |
"COMMA = ','\n", | |
"msg = MIMEMultipart('alternative')\n", | |
"msg['From'] = sender\n", | |
"msg['To'] = COMMA.join(receiver)\n", | |
"\n", | |
"msg['Subject'] = \"코드스쿼드 학습메일\"\n", | |
"msg.attach(MIMEText(body, 'html', 'utf-8'))\n", | |
"\n", | |
"# file attach\n", | |
"\n", | |
"with open(\"images/nobita.png\", \"rb\") as fp:\n", | |
" name = os.path.basename(\"images/nobita,png\")\n", | |
" part = MIMEBase(\"application\", \"octet-stream\")\n", | |
" part.set_payload(fp.read())\n", | |
" encoders.encode_base64(part)\n", | |
" part.add_header(\"Content-Disposition\",\n", | |
" \"attachment; filename=\\\"{}\\\"\".format(name))\n", | |
" msg.attach(part)\n", | |
" \n", | |
"gmail_user = \"[email protected]\"\n", | |
"\n", | |
"with open('pass.dat', 'rb') as fp:\n", | |
" gmail_pwd = pickle.load(fp)\n", | |
"\n", | |
"server = smtplib.SMTP('smtp.gmail.com', 587)\n", | |
"server.ehlo()\n", | |
"server.starttls()\n", | |
"server.login(gmail_user, gmail_pwd)\n", | |
"server.send_message(msg)\n", | |
"print(\"Successfully sent email at {}.\".format(time.asctime()))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'Tue May 30 13:15:31 2017'" | |
] | |
}, | |
"execution_count": 20, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import time\n", | |
"time.asctime()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment