python脚本,从mongo取数据发送html格式表格邮件

简介: python脚本,从mongo取数据发送html格式表格邮件

工作需要,我要把我的工作成果每隔三天发送邮件,展示三天的工作情况,所以在linux上写了个脚本,每三天发一次邮件,下面是源代码



coding: utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from config import *
from mongo_db import MongoDB
import time,datetime
import random
import json
import arrow
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
from email.header import Header
from email import encoders
#我是用当天日期作为表名存入mongo
#链接mongo进行查询
def select_mongo(data):
    pic_mongo = MongoDB(config = mongo_config).getClient().get_database('pic_db').get_collection(data)
    #查询值
    record = pic_mongo.find_one({"name": "log"})
    return record
def querydata():
    res = []
    records = []
    #获取现在的时间  "2018-01-20"
    qq = time.strftime("%Y-%m-%d")
    res.append(qq)
    #获取前两天的时间
    for x in range(1,3):
        now = arrow.get(qq)
        tomorrow = now.replace(days=-x)
        res.append(tomorrow.strftime("%Y-%m-%d"))
    print res
    sendhead = ['上传信息<br>']
    for tim in res:
        record =select_mongo(tim)
        records.append(record)
        sendhead.append('%s<br>' % tim)
    #print records
    tablecontent = '<table border="1" class="imagetable"><caption><h4>数据展示</h4></caption>'
    tablecontent += "<tr>"
    for title in sendhead:
        tablecontent += '<th>'+title+'</th>'
    tablecontent += "</tr>"
    for city_name in records[1]:
        if city_name == '_id' or city_name == 'name':
            pass
        else:
            tablecontent += '<tr>'
            tablecontent += '<td>%s</td>' % city_name
            #print city_name
            for record in records:
                count_sum = record[city_name]
                tablecontent += '<td>%s</td>' % count_sum
            tablecontent += '</tr>'
    tablecontent += '</table>'
    return tablecontent
head='''
<!DOCTYPE html>
<html>
 <head>
     <meta charset="utf-8"> 
     <title>数据展示</title>
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <!-- <meta http-equiv="refresh" content="3600">  -->
  <style type="text/css">
            table.imagetable {
            font-family: verdana,arial,sans-serif;
          font-size:11px;
          color:#333333;
          border-width: 1px;
          border-color: #999999;
          border-collapse: collapse;
            width: 800px;
        }
        table.imagetable th {
            background:#b5cfd2 url('cell-white.jpg');
            border-width: 1px;
          padding: 8px;
          border-style: solid;
          border-color: #999999;
        }
        table.imagetable td {
            background:#dcddc0 url('cell-grey.jpg');
            border-width: 1px;
          padding: 8px;
          border-style: solid;
          border-color: #999999;
            width:150px;
        }
        table.imagetable td.tou {
            background:#dcddc0 url('cell-grey.jpg');
            border-width: 1px;
          padding: 8px;
          border-style: solid;
          border-color: #999999;
            width:800px;
        }
  </style>
 </head>
 <body>
'''
tail='''
 </body>
</html>
'''
res = querydata()
msg = MIMEMultipart()
content=head+res+tail
msgtitle = '【图片展示】'
text=MIMEText(content,'html','utf-8')
msg.attach(text)
msg['from'] = 'me'
msg['to'] = '小'
msg['Subject'] = msgtitle
s = smtplib.SMTP('smtp.exmail.qq.com')
s.login('邮箱', 'smtp码')
s.sendmail('邮箱', ['收件人邮箱'], msg.as_string())
print('success')
s.close()
if __name__ == "__main__":
    content = head+res+tail
    print content


目录
相关文章
|
1月前
|
移动开发 前端开发 HTML5
Twaver-HTML5基础学习(20)数据容器(3)_数据的批量加载(节省性能方法)
本文介绍了Twaver HTML5中数据的批量加载方法,通过使用`box.startBatch()`可以在大量数据加载时提高性能。文章通过示例代码展示了如何在React组件中使用批量加载功能,以减少界面重绘次数并提升效率。
47 1
Twaver-HTML5基础学习(20)数据容器(3)_数据的批量加载(节省性能方法)
|
1月前
|
XML 存储 JSON
Twaver-HTML5基础学习(19)数据容器(2)_数据序列化_XML、Json
本文介绍了Twaver HTML5中的数据序列化,包括XML和JSON格式的序列化与反序列化方法。文章通过示例代码展示了如何将DataBox中的数据序列化为XML和JSON字符串,以及如何从这些字符串中反序列化数据,重建DataBox中的对象。此外,还提到了用户自定义属性的序列化注册方法。
39 1
|
3天前
|
XML 前端开发 数据格式
Beautiful Soup 解析html | python小知识
在数据驱动的时代,网页数据是非常宝贵的资源。很多时候我们需要从网页上提取数据,进行分析和处理。Beautiful Soup 是一个非常流行的 Python 库,可以帮助我们轻松地解析和提取网页中的数据。本文将详细介绍 Beautiful Soup 的基础知识和常用操作,帮助初学者快速入门和精通这一强大的工具。【10月更文挑战第11天】
17 2
|
12天前
HTML表格
HTML表格
36 4
|
1月前
|
XML 移动开发 JSON
Twaver-HTML5基础学习(18)数据容器(1)_增删查改、遍历数据容器、包含网元判断
本文介绍了Twaver HTML5中的数据容器(DataBox),包括如何进行增删查改操作、遍历数据容器以及判断网元是否存在于数据容器中。DataBox用于管理所有的网元对象,如ElementBox、LayerBox、AlarmBox等,并通过示例代码展示了其常用方法的使用。
42 1
Twaver-HTML5基础学习(18)数据容器(1)_增删查改、遍历数据容器、包含网元判断
|
1月前
|
移动开发 前端开发 HTML5
Twaver-HTML5基础学习(2)基本数据元素(Data)
本文介绍了Twaver HTML5中的基本数据元素,包括Data、Element、Alarm和Layer等,它们分别用来描述拓扑的网元、告警和图层。文章详细解释了Data类的基本属性和方法,并提供了如何在React组件中使用Twaver创建节点和连线的示例代码。
35 1
Twaver-HTML5基础学习(2)基本数据元素(Data)
|
1月前
|
移动开发 数据可视化 HTML5
Twaver-HTML5基础学习(40)表格可视化视图组件(Table)
本文介绍了如何在Twaver-HTML5中使用表格可视化视图组件(Table),包括创建表格、定义列对象、实现数据绑定和排序,以及处理表格事件和获取表格数据的方法。
31 1
|
1月前
|
移动开发 前端开发 JavaScript
Twaver-HTML5基础学习(3)基本数据元素(Data)其他功能函数以及组Group
本文介绍了Twaver HTML5中Data类的其他功能函数,如获取和操作子网元的方法,以及组(Group)的概念和使用。文章通过示例代码展示了如何在React组件中创建组、添加图元到组中,并通过toChildren函数获取满足特定条件的图元。
32 0
Twaver-HTML5基础学习(3)基本数据元素(Data)其他功能函数以及组Group
|
1月前
|
前端开发 JavaScript
用最少的代码实现一个HTML可交互表格
该HTML页面展示了一个可交互的表格,用户可以通过点击表格行来高亮显示所选行。使用了基本的`&lt;table&gt;`结构,并通过CSS设置了表格样式及行悬停效果。JavaScript函数`toggleSelect`实现了行选中的切换功能。
|
14天前
|
XML Web App开发 数据格式
HTML 页面显示 XML 数据
10月更文挑战第2天