python操作excel 批量发送工资信息

来源:互联网 发布:mac pages如何保存 编辑:程序博客网 时间:2024/05/16 17:07
#encoding:gbkimport sysreload(sys)sys.setdefaultencoding( "gbk" )import xlrdimport xlwtimport smtplib  import stringimport osfrom 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_listtry:  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 += xreturn retdef 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.nrowsncols = table.ncolscollist = []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 = 7tlist = []for x in collist:tlist.append(table.cell(0,x).value)for x in tlist:print xfor 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 + "," + msgsndmsg_bad =  "send failed : "  +  mail + "," + msgif send_mail(mail,"salary",msg):print sndmsg_oksfile.write(sndmsg_ok + '\r')else:print sndmsg_badsfile.write(sndmsg_bad + '\r')sfile.close()if __name__ == "__main__":t = Excel()t.do_work("h:\\test.xls","test")



test.xls内容如下



0 0
原创粉丝点击