Python 3在工作中的使用
安裝配置Python 3
安裝
- 首先確保在python36的Script文件夾路徑下執行命令。或者,最好將Windows環境變量設置為python.exe所在路徑和pip所在路徑。
- python> pip install pyodbc
pip 命令
- 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中設置或通過菜單設置。
使用sql server數據庫
連接SQL Server數據庫
由於pymssql暫時不支持python3,無法使用;發現可以通過pyodbc連接SQL Server數據庫。
訪問數據庫
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
相關的包:
- xlrd
- xlwt
- xlutils
讀取Excel - xlrd包
https://www.cnblogs.com/miniren/p/5763931.html
寫入Excel - xlwt包
參考:https://www.cnblogs.com/miniren/p/5763931.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()
https://www.cnblogs.com/Devopser/p/6366975.html
