, this example is for you to share the specific code of the python3 excel bulk sending excel appendix for your reference. The details are as follows:

connection, query mysql, import to excel file, send e-mail and accessories regularly.
mainly used in pymysql, smtplib,

#1, MySQL module xlwt


 import pymysql into #Python3 Python2, mysqldb import OS import datetime # is regularly sent, and the date of import shutil import smtplib # # file operation module from email.mime.text import MIMEText from message email.mime.multipart import MIMEMultipart from email.header import Header import time import xlwt #excel 

#2 writing mysql

 def, the connection and query eMysql (mysql_host, mysql_port, mysql_user, mysql_password, mysql_db, SQL): try: DB = pymysql.connect (host=mysql_host, port=mysql_port, user=mysql_user, password=mysql_password, db=mysql_db, charset ='utf8') # connection database encoding note utf8, or Chinese output will be garbled print ("MYSQL CONNECTED. "cursor = db.cursor) # database connection (#) using cursor () method to obtain operating cursor cursor.execute (SQL) print ('SQL # execute SQL statements EXECUTED') results = cursor.fetchall (return results print) # results ('RESULTS EXECUTED') db.close (print) # closes the database connection ('MYSQL CLOSED'except: print ('SQL FAILED') 

#3, excel

) to def eWrite (fLocate, results, file_sheet, file_subject, style0): try: if os.path.exists (fLocate): os.remove (fLocate) # if the file exists, then delete the F = xlwt.Workbook (encoding='utf-8') # open excel file FS = f.add_sheet (file_sheet) #sheet subject = list (file_subject) # the list of for I in range (len (subject)): # find the date column' in subject[i] if 'date: col _num=i for I in range (len (subject) fs.write (0): #sheet title, I subject[i], for I in (len) range (results)): #sheet data format for the date for date column (len J in range (results[0]) if j== col_num: (fs.write): I + 1, J, results[i][j] style0 fs.write (else:), I + 1, J, results[i][j]) for I in range (10): # cell width is fs.col (I).Width=3333 print ("WRITE FINISHED") f.save (fLocate) except: Print ("WRITE FAILED") 


 def eSend (email sender, receiver username, password, smtpserver, subject, e_content, file_path, file_name, try:): Message = MIMEMultipart (# headers) message['From'] = sender# message['To'] = ".Join" to send (receiver) # collection message['Subject'] = Header (subject, 'utf-8'(message.attach) MIMEText (e_content,'plain','utf-8')) # text # structure (open = MIMEText annex att1 (file_path+file_name,'rb') (.Read),'base64','utf-8') att1["Content-Type"] ='application/octet-stream'att1[] "Content-Disposition" = "attachment; filename=" +file_name message.attach (att1) SMTP = # (smtplib.SMTP) smtp.connect (smtpserver) # connection server smtp.login (username, password) smtp.sendmail (sender, receiver # login (message.as_string), smtp.quit (print) # sent) ("SEND") except: print ("SEND FAILED") 


 while True: configuration and implementation of # configuration #__time_____ ehour=5# timing emin=21# hours timing esec=41# timing minutes seconds current_time = time.localtime (time.time ()) # when The time before date cur_time = time.strftime ('%H%M', time.localtime (time.time) (STR)) # current time #__mysql_____ mysql_host = mysql_host host mysql_port =mysql_port # # login login port mysql_user = mysql_user mysql_password = mysql_password # # login password mysql_db = mysql_db # database SQL = sql.encode ('utf-8') #sql query #__email_____ sender = sender # encoding receiver = ['453032441@qq.com'] # from email mailbox, multiple (list) mass # username = username password = password # sender #smtp password, QQ is assigned a string to you, 163 is to set up their own smtpserver = smtpserver # mailbox server subject = "Hey here's something interesting," # mail Title e_content ='{0:^27}n{1:^27}n{2:^2 5}n{3:^25}'.format ('i'(-) /'re,' ',' '(-)) # text # text #__file_____ file_path = "D:/" file_name= "shit.xls" # file location # file name fLocate = file_path + file_name # file path file_subject='Gave','you','a','piece','of','shit.'#sheet file_sheet='ok' #sheet Title style0=xlwt.XFStyle (style0.num_format_str='YYYY-MM-DD') if (# operation (current_time.tm_hour = = ehour) and (current_time.tm_min = = Emin) and (current_time.tm_sec = = ESEC)): Print (START) results=eMysql (mysql_host, mysql_port, mysql_user, mysql_password, mysql_db, SQL) eWrite (fLocate, results, file_sheet, file_subject, style0 (sender, eSend) receiver, username, password, smtpserver, subject, e_content, file_path, file_name (cur) print _time) time.sleep (1) 

above is the whole content of this article, I hope it will help you learn, and I hope you will support the script home.

This paper fixed link:http://www.script-home.com/python3mysql-query-data-and-send-excel-attachments-by-mail.html | Script Home | +Copy Link

Article reprint please specify:Python3+mysql query data and send excel attachments by mail | Script Home

You may also be interested in these articles!