美文网首页
python魔术命令及如何自动化发送邮件

python魔术命令及如何自动化发送邮件

作者: 这是沸羊羊的干爹 | 来源:发表于2018-11-16 18:23 被阅读0次

    分析建模,日常问题整理(十六)


    2018.11.12~2018.11.18


    • 1 自动转化jupyter文件为html

    首先安装nbconvert模块
    命令行运行jupyter cmd:jupyter notebook
    方法一:
    > cmd进入anaconda/script
    > 执行:jupyter-nbconvert.exe --to html D:\\test.ipynb
    方法二:
    在jupyternote网页中输入
    ! jupyter nbconvert C:\\Users\\Administrator\\Untitled.ipynb
    ! jupyter nbconvert "C:\Users\Administrator\Untitled.ipynb"
    ! jupyter nbconvert --to html "C:\Users\Administrator\Untitled2.ipynb"
    原因是command是unix的?在ipython里面要用!符号?
    注意1:ipython nbconvert和jupyter nbconvert,也可以用ipython nbconvert
    注意2:输入要转化的文件的路径,而非工作路径

    • 2 python中$符号什么意思

    $是用来表示unix下命令行的提示符
    !的作用在windows里面,在Python的IDE里面的某些命令行前需要添加!

    • 3 魔术命令

    1.%quickref ipython 快速参考
    2.%run script.py 执行py文件,加载脚本。import pandas as pd 加载模块
    3.%time print('hello world') 输出命令行的运行时长
    4.%timeit print('hello world') 输出命令行的运行时长
    5.%%time def func_():print('hello world') 输出代码块的运行时长
    6.%matplotlib inline 确保jupyter展示绘图
    7.%system date 操作shell(Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。
    Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务),输出系统时间。
    8.!!date%system date等价
    9.%who_ls展示工作环境中的变量
    10.%who_ls function展示工作环境中的函数
    11.%whos查看空间所有变量的信息级别

    微信截图_20181115134406.png

    12.%hist打开命令的输入历史,运行过的代码?可以用来找回刚误删的代码。
    魔术命令文档
    %magic查看所有魔术命令的详细文档

    • 4 python发送邮件和附件(图片、txt、HTML、等格式的文件)
      (1)发送文本
    from email import encoders
    from email.header import Header
    from email.mime.text import MIMEText
    import  smtplib
    
    msg=MIMEText('邮件内容','plain','utf-8')
    msg['From']=Header('发件人姓名','utf-8')
    msg['To']=Header('收件人姓名','utf-8')
    msg['Subject']=Header('邮件主题','utf-8')
    
    server=smtplib.SMTP('smtp.qq.com',25)
    server.set_debuglevel(1)
    server.starttls()
    server.login('my@qq.com','smtp验证号')
    server.sendmail('my@qq.com',['his@qq.com'],msg.as_string())
    server.quit()
    

    参考
    (2)发送附件

    from email import encoders
    from email.header import Header
    from email.mime.text import MIMEText  # 可以是html格式的,比较美观
    from email.mime.multipart import MIMEMultipart
    from email.mime.base import MIMEBase
    import  smtplib
    import warnings
    warnings.filterwarnings('ignore')
    from email.mime.image import MIMEImage
    from email.mime.application import MIMEApplication ##用于存放文件
    
    
    msg = MIMEMultipart()  # 构造容器
    msg['From']=Header('发件人的名字','utf-8')
    msg['To']=Header('收件的名字','utf-8')
    msg['Subject']=Header('这是邮件主题','utf-8')  
    msg.attach(MIMEText('''
    <html>
        <body>
            <h3>一封pyhon自动发送图片和附件的测试邮件</h3>
            <p><img src="cid:0" /></p>
        </body>
    </html>
    ''', 'html', 'utf-8'))
     
    #  添加邮件内容,收件人姓名、发件人姓名、邮件主题和邮件内容
        
    # 添加附件就是加上一个MIMEBase,从本地读取一个图片:
    with open('''D:\\Penguins.jpg''', 'rb') as f:
        # 设置附件的MIME和文件名,jpg:
        mime = MIMEBase('image', 'jpg', filename='Penguins.jpg')
        # 加上必要的头信息:这个头信息是什么东西
        mime.add_header('Content-Disposition', 'attachment', filename='Penguins.jpg')
        mime.add_header('Content-ID', '<0>')
        mime.add_header('X-Attachment-Id', '0')
        # 把附件的内容读进来:
        mime.set_payload(f.read())
        # 用Base64编码:
        encoders.encode_base64(mime)
        # 添加到MIMEMultipart:
        msg.attach(mime)
    f.close()
    
    part1 = MIMEApplication(open('D:\\filename.txt','rb').read())
    part1.add_header('Content-Disposition', 'attachment', filename="filename.txt")
    msg.attach(part1)
    
    part2 = MIMEApplication(open('D:\\filename.html','rb').read())
    part2.add_header('Content-Disposition', 'attachment', filename="filename.html")
    msg.attach(part2)
    
    server=smtplib.SMTP('smtp.qq.com',25)  # smtp服务器需要验证
    # server.set_debuglevel(1)  # 开启发送debug模式,把发送邮件的过程显示出来
    server.starttls() # 启动安全传输模式
    server.login('my@qq.com','stmp验证密码')  #  登录邮箱
    server.sendmail('my@qq.com',['her@qq.com'],msg.as_string())   #  填写收件人和发件人以及发送内容
    server.quit()
    
    • 5 jupyter的markdown

    字体、大小:
    <font face="微软雅黑",size=4> 报表说明 </font>
    背景颜色:
    <table><tr><td bgcolor=orange> 背景色是:orange </td></tr></table>
    公式:$公式$会输出标准美观的公式。
    $1+2$1+2

    相关文章

      网友评论

          本文标题:python魔术命令及如何自动化发送邮件

          本文链接:https://www.haomeiwen.com/subject/ohxbxqtx.html