|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
萌新一个,现在想用python搭建一个自动化发报表的小程序,遇到了一个问题:这个代码有时候可以运行,有时候又会报错(通常循环没运行完中途报错),报错内容比较奇怪,打开模块包代码也开不懂,所以前来求助。报错的区域我用红字标记了,麻烦大佬们帮帮忙看看,报错内容我已经放上去了。
import win32com.client
from PIL import ImageGrab
import uuid
import time
import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
from email.mime.base import MIMEBase
from email.header import Header
#日期函数
now = datetime.datetime.now()
date=datetime.datetime.strftime(now-datetime.timedelta(days = 1), '%Y%m%d')
# 截图邮件正文内容
def copypicture(file_path,sheet_name,range):
w = win32com.client.Dispatch('Excel.Application')
wb = w.Workbooks.Open(file_path)
ws = wb.Worksheets(sheet_name)
ws.Range(range).CopyPicture()
ws.Paste(ws.Range('Z1'))
new_shape_name = sheet_name
w.Selection.ShapeRange.Name = new_shape_name
ws.Shapes(new_shape_name).Copy()
img = ImageGrab.grabclipboard()
img.save ('C:\\Users\\lujinjian\Desktop\\粤南大区续费报表\\全网通明细\\'+sheet_name +str(date)+'.png')
wb.SaveAs ('C:\\Users\\lujinjian\Desktop\\粤南大区续费报表\\全网通明细\\'+sheet_name+str(date)+'.xlsx')
wb.Close()
print(sheet_name+"已经保存")
def sent_mail(user_name,receivers_name,password,city,picture_file_path,file_path):
user = user_name #发送人邮件
pwd = password #密码
receivers = receivers_name #收件人
msg = MIMEMultipart()
msg.attach(MIMEText('<p><img src="cid:1"></p>', 'html', 'utf-8'))
fp1 = open(picture_file_path, 'rb')
msgImage = MIMEImage(fp1.read())
fp1.close()
msgImage.add_header('Content-ID', '<1>')
msg.attach(msgImage)
#添加附件
att = MIMEApplication(open(file_path,'rb').read())
att.add_header('Content-Disposition', 'attachment', filename=Header(city +date+'.xlsx','utf-8').encode())
msg.attach(att)
#添加收件发件人信息
msg['From'] = "{}".format(user)
msg['To'] = receivers_name
msg['Subject'] = city+ '续费日报_'+date
#发送邮件
try:
smtp = smtplib.SMTP()
smtp.connect('smtp.exmail.qq.com')
smtp.login(user,pwd)
smtp.sendmail(user, receivers, msg.as_string())
smtp.close()
print("邮件发送成功")
except:
print("邮件发送不成功")
#循环操作
city_fasong = {'惠州':'B1:T200','南宁':'B1:T248','中山':'B1:T164','珠海':'B1:T128','东莞':'B1:T265'}
user_name = ('lujinjian@58.com')
yy_receivers = ('lujinjian@58.com,wangxiao19')
password = 'cHScQqNjoExNcPXR'
city_receivers ={'东莞':'lujinjian@58.com','惠州':'wangxiao19','南宁':'wangxiao19','中山':'wangxiao19','珠海':'wangxiao19'}
for key in city_fasong.keys():
city = key
file_path =('C:\\Users\\lujinjian\Desktop\\粤南大区续费报表\\全网通明细\\'+city+'.xlsx')
value = city_fasong[key]
copypicture(file_path,key,value)
picture_file_path = ('C:\\Users\\lujinjian\Desktop\\粤南大区续费报表\\全网通明细\\'+city +str(date)+'.png')
file_path = ('C:\\Users\\lujinjian\Desktop\\粤南大区续费报表\\全网通明细\\'+city +str(date)+'.xlsx')
all_receivers = yy_receivers + city_receivers[key]
sent_mail(user_name,all_receivers,password,city,picture_file_path,file_path)
输出内容:
惠州已经保存
邮件发送成功
Traceback (most recent call last):
File "C:\Users\lujinjian\Desktop\粤南大区续费报表\全网通明细\测试.py", line 73, in <module>
copypicture(file_path,key,value)
File "C:\Users\lujinjian\Desktop\粤南大区续费报表\全网通明细\测试.py", line 26, in copypicture
img.save ('C:\\Users\\lujinjian\Desktop\\粤南大区续费报表\\全网通明细\\'+sheet_name +str(date)+'.png')
AttributeError: 'NoneType' object has no attribute 'save' |
|