#encoding:gbk
import sys
reload(sys)
sys.setdefaultencoding( "gbk" )
import xlrd
import xlwt
import smtplib
import string
import os
from email.mime.text import MIMEText
mail_host="smtp.163.com"
mail_user="ccc"
mail_pass="cccpwd"
mail_postfix="163.com"
def send_mail(to_list,sub,content):
me="hello"+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content,_subtype='plain',_charset='gb2312')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = to_list
try:
server = smtplib.SMTP()
server.connect(mail_host)
server.login(mail_user,mail_pass)
server.sendmail(me, to_list, msg.as_string())
server.close()
print "".join(content)
return True
except Exception, e:
print "exception ......\r"
print str(e)
return False
def list_2_string(lst ):
ret = ""
for x in lst:
if type(x) is not string:
ret += str(x)
else:
ret += x
return ret
def vonvert(a,b):
return ord(a) - ord(b)
class Excel():
def do_work(self, inEfile, outfile):
rfile = xlrd.open_workbook(inEfile)
table = rfile.sheet_by_index(0x01)
table.row_values(0x00)
table.col_values(0x00)
nrows = table.nrows
ncols = table.ncols
collist = []
collist.append(0x00)
collist.append(0x01)
collist.append(0x02)
collist.append(0x03)
collist.append(0x04)
collist.append(0x05)
collist.append(0x06)
collist.append(0x07)
sfile = open("result.txt",'w')
colmail = 7
tlist = []
for x in collist:
tlist.append(table.cell(0,x).value)
for x in tlist:
print x
for idx in range(2,7):
mail = str(table.cell(idx,colmail).value)
content = []
for x in range(0,6):
content.append(table.cell(idx,x).value)
msg = ""
for y in range(len(content)):
msg += str(tlist[y])
msg += " : "
msg += str(content[y])
msg += "\n"
sndmsg_ok = "send ok : " + mail + "," + msg
sndmsg_bad = "send failed : " + mail + "," + msg
if send_mail(mail,"salary",msg):
print sndmsg_ok
sfile.write(sndmsg_ok + '\r')
else:
print sndmsg_bad
sfile.write(sndmsg_bad + '\r')
sfile.close()
if __name__ == "__main__":
t = Excel()
t.do_work("h:\\test.xls","test")
test.xls内容如下