截图模块
#截图模块 def get_snapshot(): #定义变量 #excel文件的绝对位置 excel_file=r'E:\L.xlsx' #输出图片的绝对位置 output_png_path=r'E:\L.png' #excel的sheet名称 excel_tb='每日销售' #注意要装excel软件,否则会报错 excel = win32.Dispatch('Excel.Application') wb = excel.Workbooks.Open(excel_file) ws = wb.WorkSheets(excel_tb) # 打开工作簿 # 示例:截图的起始终止格 start_cell = "A1" end_cell = "B48" #存放图片在excel中,否则直接剪贴板会报错 paste_cell= "Z99" ws.Range(f"{start_cell}:{end_cell}").CopyPicture() # 变成图片 ws.Paste(ws.Range(paste_cell)) # 将图片黏贴在excel中 ws.Shapes(ws.Shapes.Count).Copy() # 图片至剪贴板 img = ImageGrab.grabclipboard() # 从剪贴板获取图片 img.save(output_png_path) # 图片保存 wb.Save() # excel保存 wb.Close() print('输出图片:'+output_png_path)
邮件正文发送图片,并附带附件
1 def attach_mail_module(mail_title,mail_content,mail_to_list,mail_ccto_list,attachment,attachment_file_name): 2 print('==进入attach_mail_module模块==') 3 4 #读取配置与密码,所有邮件信息都从配置文件来 5 conf_file='e:\\p.txt' 6 conf = configparser.ConfigParser() 7 conf.read(conf_file,encoding="utf-8") 8 mail_conf="mail_address_info" 9 10 abnormal_mail_reciever = conf.get(mail_conf,"mail_reciever") 11 smtp_address_ip = conf.get(mail_conf,"smtp_address_ip") 12 smtp_port = int(conf.get(mail_conf,"smtp_port")) 13 login_user_name = conf.get(mail_conf,"login_user_name") 14 login_passwd = conf.get(mail_conf,"login_passwd") 15 send_mail_from = conf.get(mail_conf,"send_mail_from") 16 17 #设置主题,收件人,抄送人 18 msg = MIMEMultipart('mixed') 19 msg['Subject'] = Header(mail_title,'utf-8') 20 msg['From'] = send_mail_from 21 msg['To'] = ",".join(to_list) 22 msg['cc'] = ','.join(ccto_list) 23 receive = to_list 24 receive.extend(ccto_list) 25 26 #设置附件(可以是中文名) 27 att = MIMEText(open(attachment, 'rb').read(), 'base64', 'utf-8') 28 att["Content-Type"] = 'application/octet-stream' 29 att.add_header("Content-Disposition", "attachment", filename=("gbk", "", attachment_file_name+".xlsx")) 30 msg.attach(att) 31 32 #设置图片 33 content=mail_content 34 html_img = f'<p>{content}<br><img src="cid:image1"></br></p>' # html格式添加图片 35 f = open(r"E:\LHD6_base.png", 'rb') #打开图片 36 msgimage = MIMEImage(f.read()) 37 f.close() 38 msgimage.add_header('Content-ID', '<image1>') # 设置图片 39 msg.attach(msgimage) 40 msg.attach(MIMEText(html_img,'html','utf-8')) # 添加到邮件正文 41 42 #发送邮件 43 smtp = smtplib.SMTP() 44 smtp.connect(smtp_address_ip,smtp_port) 45 smtp.login(login_user_name,login_passwd) 46 smtp.sendmail(send_mail_from, receive , msg.as_string()) 47 smtp.quit() 48 print('==attach_mail_module结束==\n')