使用gspread获取逗号分隔的单元格值列表

2024-03-28 14:15:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用谷歌电子表格中储存的电子邮件地址发送邮件。我可以得到单元格值,但我得到:

(<Cell R1C1 'email@gmail.com'>,) (<Cell R2C1 'email1@gmail.com'>,) ...

为了使用MIMEMultipart,我需要一个逗号分隔: 邮箱:gmail.com,电子邮件1gmail.com,... 你知道吗

这是我的密码:

import gspread import json from oauth2client.client import SignedJwtAssertionCredentials import gdata.spreadsheet.service from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText import smtplib json_key = json.load(open('xxx.json')) scope = ['https://spreadsheets.google.com/feeds'] credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope) gc = gspread.authorize(credentials) wks = gc.open('xxx') worksheet_id = 'od6' worksheet = wks.get_worksheet(0) print worksheet.row_values("A1") for x in xrange(1,50): test = "A%s" %x msg = MIMEMultipart() msg["To"] = worksheet.acell(test), print msg["To"]

Tags: keyfromimportcomclientjson电子邮件email
1条回答
网友
1楼 · 发布于 2024-03-28 14:15:00

如果有人想要我找到的解决方案:

import gspread
import json
from oauth2client.client import SignedJwtAssertionCredentials
import gdata.spreadsheet.service
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib

json_key = json.load(open('AUTH2.json'))
scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)
gc = gspread.authorize(credentials)

wks = gc.open('Name-of-your-spreadsheet')
worksheet_id = 'od6'
worksheet = wks.get_worksheet(0)
server = smtplib.SMTP('smtp.gmail.com:25')
server.starttls()
server.login('LOGIN','PSWD')

for x in xrange(1,20):
    getTheCellName = "A%s" %x
    msg = MIMEMultipart()
    getTheCell = worksheet.acell(getTheCellName)
    getTheCellValue = getTheCell.value
    if getTheCellValue != "":
        server.sendmail('Sender-Email',getTheCellValue,'Mail-Content')

相关问题 更多 >