python 3 在工做中的應用

Python 3在工做中的使用html


 

安裝配置Python 3

安裝python

  1. 首先確保在python36的Script文件夾路徑下執行命令。或者,最好將Windows環境變量設置爲python.exe所在路徑和pip所在路徑。
  2. python> pip install pyodbc

pip 命令sql

  • pip install package-name #安裝軟件包
  • pip list # 顯示pip安裝的軟件包列表
  • pip show package-name # 顯示軟件包的信息

在notepad++中配置Python 3

在notepad++的程序根目錄下,編輯shortcuts.xml文件。在 UserDefinedCommands節點下輸入:數據庫

<Command name="python 3" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /k python $(FULL_CURRENT_PATH)</Command>

 

而後,編寫並保存python程序*.py,經過點擊菜單上的"運行">"python 3"便可經過python執行程序。微信

另外,若是須要使用快捷鍵啓動,也能夠在上面的xml中設置或經過菜單設置。app

使用sql server數據庫

鏈接SQL Server數據庫測試

因爲pymssql暫時不支持python3,沒法使用;發現能夠經過pyodbc鏈接SQL Server數據庫。fetch

訪問數據庫ui

1 import pyodbc 2 conn = pyodbc.connect('Driver={SQL Server};Server=GCDC-SQLTEST01;Database=gconline;uid=isystem;pwd=isystem') 3 cur = conn.cursor() 4 cur.execute("select top 100 * from agent") 5 row = cur.fetchone() 6 row[0]

 

操做Excel

相關的包:spa

  • xlrd
  • xlwt
  • xlutils

讀取Excel - xlrd包

http://www.javashuo.com/article/p-eqqhguyp-c.html

寫入Excel - xlwt包

參考:http://www.javashuo.com/article/p-eqqhguyp-c.html

1 import xlwt 2 new_workbook = xlwt.Workbook() 3 new_sheet=new_workbook.add_sheet("pySheet1") 4 new_sheet.write(0,0,"hello") 5 new_sheet.write(2,0,5) 6 new_sheet.write(2,1,8) 7 new_sheet.write(3,0,xlwt.Formula("A3+B3")) 8 new_workbook.save(r"D:\pyCreateWorkbook.xls")

 

D盤下excel文件結果

 

A

B

C

...

1

hello

     

2

       

3

5

8

   

4

13

     

 

使用郵件

發送Email (email.mycompany.com)

https://www.cnblogs.com/vivivi/p/5952093.html

http://blog.csdn.net/u013511642/article/details/44251799 (帶附件)

http://www.runoob.com/python3/python3-smtp.html

發送通常文本郵件

1 import smtplib 2 from email.mime.multipart import MIMEMultipart 3 msg=MIMEMultipart() 4 msg['subject']='This is the email\'s subject'
5 msg['from']='peter@mycompany.com'
6 msg['to']='peter@mycompany.com;alice@mycompany.com'
7 s=smtplib.SMTP('mail.mycompany.com') 8 s.send_message(msg) #觸發發送郵件動做
9 s.quit()

 

另外,yagmail包發送郵件很方便,可是很遺憾exchange暫時沒法使用。

發送HTML格式郵件

 1 import smtplib  2 from email.mime.text import MIMEText  3 content_msg = '''
 4  <p>這是一封<strong>HTML</strong>文本郵件</p>  5  <a href="https://wx.qq.com/" title="點擊打開">微信網頁版</a>  6 '''
 7 msg=MIMEText(content_msg,'html','utf-8')  8 msg['subject']='This is the email\'s subject'
 9 msg['from']='peter@mycompany.com'
10 msg['to']='peter@mycompany.com;alice@mycompany.com'
11 s=smtplib.SMTP('mail.mycompany.com') 12 s.send_message(msg) #觸發發送郵件動做
13 s.quit()

 

發送帶附件的郵件

 1 import smtplib  2 from email.mime.text import MIMEText  3 from email.mime.multipart import MIMEMultipart  4 msg=MIMEMultipart()  5 msg['from']='peter@mycompany.com'
 6 msg['to']='peter@mycompany.com;alice@mycompany.com'
 7 msg['subject']='經過python 3發送的測試郵件'
 8 msg.attach(MIMEText('這是一封測試郵件,請忽略','plain','utf-8'))  9 att1 = MIMEText(open('D:\\pyCreateWorkbook.xls','rb').read(),'base64','utf-8') 10 att1["Content-Type"]='application/octet-stream'
11 att1["Content-Disposition"]='attachment;filename="BJ.xls"'
12 msg.attach(att1) 13 s=smtplib.SMTP('mail.mycompany.com') 14 s.send_message(msg) #觸發發送郵件動做
15 s.quit()

 

Python 3 日誌記錄

https://www.cnblogs.com/Devopser/p/6366975.html

 

原文出處:https://www.cnblogs.com/iPeterRex/p/10930882.html

相關文章
相關標籤/搜索