Skip to content

Commit 1b17e8a

Browse files
committed
Merge branch 'master' of github.com:JustDoPython/python-examples
2 parents 41bcc0b + bdf99d6 commit 1b17e8a

File tree

15 files changed

+310
-2
lines changed

15 files changed

+310
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
Python技术 公众号文章代码库
44

55

6-
关注公众号:python 技术,回复"python"一起学习交流
6+
关注公众号:python技术,回复"python"一起学习交流
77

88
![](http://favorites.ren/assets/images/python.jpg)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import smtplib
2+
from email.mime.text import *
3+
from email.utils import formataddr
4+
5+
my_sender = 'xxxxx@qq.com' # 发送方邮箱
6+
my_psw = 'xxxxxxxxxxx' # 填入发送方邮箱的授权码
7+
my_user = 'xxxx@qq.com' # 收件人邮箱
8+
9+
10+
def send_email():
11+
ret = True
12+
try:
13+
msg = MIMEText('待花开时,邀您一起赏花吃热干面,我们重新拥抱这座城市的热情', 'plain', 'utf-8')
14+
15+
msg['From'] = formataddr(["知心。。。。", my_sender]) # 发件人邮箱昵称、发件人邮箱账号
16+
msg['To'] = formataddr(["知心。。。。", my_user]) # 收件人邮箱昵称、收件人邮箱账号
17+
msg['Subject'] = "静待归期!" # 邮件主题
18+
19+
server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器,端口是25
20+
21+
server.login(my_sender, my_psw) # 发件人邮箱账号、邮箱密码
22+
server.sendmail(my_sender, [my_user, ], msg.as_string()) # 发件人邮箱账号、授权码、收件人邮箱账号、发送邮件
23+
server.quit() # 关闭连接
24+
except Exception: # 如果 try 中的语句没有执行,则会执行下面的 ret=False
25+
ret = False
26+
return ret
27+
28+
ret = send_email()
29+
if ret:
30+
print(ret)
31+
print("邮件发送成功")
32+
else:
33+
print(ret)
34+
print("邮件发送失败")
35+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import smtplib
2+
from email.mime.text import MIMEText
3+
from email.mime.multipart import MIMEMultipart
4+
from email.header import Header
5+
6+
7+
my_sender = 'xxxxx@qq.com' # 发送方邮箱
8+
my_psw = 'xxxxxxxxxxx' # 填入发送方邮箱的授权码
9+
my_user = 'xxxx@qq.com' # 收件人邮箱
10+
11+
12+
# 创建一个带附件的实例
13+
message = MIMEMultipart()
14+
message['From'] = Header("潮汐同学", 'utf-8')
15+
message['To'] = Header("武汉人民", 'utf-8')
16+
subject = '荆楚疫情去'
17+
message['Subject'] = Header(subject, 'utf-8')
18+
19+
# 邮件正文内容
20+
message.attach(MIMEText('南山镇守江南之都,且九州一心!月余,疫尽去,举国庆之!', 'plain', 'utf-8'))
21+
# 构造附件1,传送当前目录下的 test.txt 文件
22+
att1 = MIMEText(open('./test.txt', 'rb').read(), 'base64', 'utf-8')
23+
att1["Content-Type"] = 'application/octet-stream'
24+
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
25+
att1["Content-Disposition"] = 'attachment; filename="test.txt"'
26+
message.attach(att1)
27+
28+
try:
29+
server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器,端口是25
30+
31+
server.login(my_sender, my_psw) # 发件人邮箱账号、邮箱密码
32+
server.sendmail(my_sender, my_user, message.as_string())
33+
server.quit() # 关闭连接
34+
print("邮件发送成功")
35+
except smtplib.SMTPException:
36+
print("Error: 无法发送邮件")
37+
1.33 MB
Loading
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import smtplib
2+
from email.mime.text import MIMEText
3+
from email.header import Header
4+
from email.mime.multipart import MIMEMultipart
5+
from email.mime.text import MIMEText
6+
from email.mime.image import MIMEImage
7+
8+
my_sender = 'xxxxx@qq.com' # 发送方邮箱
9+
my_psw = 'xxxxxxxxxxx' # 填入发送方邮箱的授权码
10+
my_user = 'xxxx@qq.com' # 收件人邮箱
11+
12+
13+
def send():
14+
subject = "解封纪念日" # 主题
15+
msg = MIMEMultipart('related')
16+
content = MIMEText('<html><body><img src="cid:imageid" alt="imageid"></body></html>', 'html', 'utf-8') # 正文
17+
# msg = MIMEText(content)
18+
msg.attach(content)
19+
msg['From'] = Header("潮汐同学", 'utf-8')
20+
msg['To'] = Header("武汉人民", 'utf-8')
21+
msg['Subject'] = Header(subject, 'utf-8')
22+
23+
file = open("./picture.png", "rb")
24+
img_data = file.read()
25+
file.close()
26+
27+
img = MIMEImage(img_data)
28+
img.add_header('Content-ID', 'imageid')
29+
msg.attach(img)
30+
31+
try:
32+
s = smtplib.SMTP_SSL("smtp.qq.com", 465) # 邮件服务器及端口号
33+
s.login(my_sender, my_psw)
34+
s.sendmail(my_sender, my_user, msg.as_string())
35+
print("邮件发送成功")
36+
except smtplib.SMTPException:
37+
print("Error: 无法发送邮件")
38+
39+
if __name__ == '__main__':
40+
send()

chaoxi/2020-04-07-send_email/test.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
多年后,后人们提起:
2+
己亥末,庚子春,荆楚大疫,
3+
染者数万,众惶恐,举国防,
4+
皆闭户,道无车舟,万巷空寂。
5+
然,外狼亦动,垂涎而候,华夏腹背芒刺。
6+
幸龙魂不死,风雨而立!医无私,警无畏,民齐心。
7+
政者,医者,兵者,扛鼎逆行勇战矣!
8+
商客,名家,百姓,仁义者,邻邦献物捐资。
9+
叹山川异域,风月同天,岂曰无衣,与子同裳!
10+
能者竭力,万民同心。
11+
月余,疫除,终胜。
12+
此后百年,风调雨顺,国泰民安!

chaoxi/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
+ [feiyan_data](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/2020-02-24-feiyan_data) :肺炎数据抓取并展示
44
+ [cherry_tree](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/2020-03-24-cherry_tree) :Python 樱花小技
55
+ [jupyter_notebook](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/2020-03-30-jupyter_notebook) :Python Jupyter notebook 操作
6+
+ [send_email](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/2020-04-07-send_email) :今天,我用 Python 给武汉人民发一封邮件
67

78
---
89

taiyangxue/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- [timefriend](https://github.com/JustDoPython/python-examples/tree/master/taiyangxue/timefriend) :做时间的朋友 —— 用印象笔记打造时间记录工具
44
- [pythondocx](https://github.com/JustDoPython/python-examples/tree/master/taiyangxue/pythondocx) :Word 神器 python-docx
5+
- [pythonexcel](https://github.com/JustDoPython/python-examples/tree/master/taiyangxue/pythonexcel) :Excel 神器 OpenPyXl
56

67
---
78

taiyangxue/pythonxlsx/app.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
from openpyxl import Workbook
2+
3+
def createWorkbook():
4+
# 创建一个 workbook 对象
5+
wb = Workbook()
6+
# 得到激活的 sheet
7+
ws = wb.active
8+
# 单元个设置值
9+
ws['A1'] = 42
10+
# 批量设置行
11+
ws.append([1, 2, 3])
12+
13+
wb.save('createWorkbook.xlsx')
14+
15+
def loadWorkbook():
16+
from openpyxl import load_workbook
17+
wb = load_workbook('sample.xlsx')
18+
print("sample.xlsx 活动 sheet 第一个单元格值为:", wb.active['A1'].value)
19+
20+
def operateCell():
21+
wb = Workbook()
22+
ws = wb.active
23+
ws.append((1,2,3))
24+
ws.append((11,22,33))
25+
ws.append((111,222,333))
26+
27+
# 操作单列
28+
for cell in ws["A"]:
29+
print(cell.value)
30+
# 操作单行
31+
for cell in ws["1"]:
32+
print(cell.value)
33+
# 操作多列
34+
for column in ws['A:C']:
35+
for cell in column:
36+
print(cell.value)
37+
# 操作多行
38+
for row in ws['1:3']:
39+
for cell in row:
40+
print(cell.value)
41+
# 指定范围
42+
for row in ws['A1:C3']:
43+
for cell in row:
44+
print(cell.value)
45+
46+
def setCellFormat():
47+
# 单元格格式
48+
from openpyxl.styles import Font, PatternFill, Border, Side, Alignment, Protection
49+
from openpyxl.styles import numbers
50+
51+
wb = Workbook()
52+
ws = wb.active
53+
ws.cell(row=1, column=1, value='宋体').font = Font(name=u'宋体', size=12, bold=True, color='FF0000')
54+
ws.cell(row=2, column=2, value='右对齐').alignment = Alignment(horizontal='right')
55+
ws.cell(row=3, column=3, value='填充渐变色').fill = PatternFill(fill_type='solid', start_color='FF0000')
56+
ws.cell(row=4, column=4, value='设置边线').border = Border(left=Side(border_style='thin', color='FF0000'), right= Side(border_style='thin', color='FF0000'))
57+
ws.cell(row=5, column=5, value='受保护的').protection = Protection(locked=True, hidden=True)
58+
ws.cell(row=6, column=6, value=0.54).number_format =numbers.FORMAT_PERCENTAGE
59+
wb.save('setCellFormat.xlsx')
60+
print('打开文件 setCellFormat.xlsx 查看结果')
61+
62+
def barChart():
63+
from openpyxl import Workbook
64+
from openpyxl.chart import BarChart, Reference
65+
66+
wb = Workbook()
67+
ws = wb.active
68+
69+
rows = [
70+
('月份', '苹果', '香蕉'),
71+
(1, 43, 25),
72+
(2, 10, 30),
73+
(3, 40, 60),
74+
(4, 50, 70),
75+
(5, 20, 10),
76+
(6, 10, 40),
77+
(7, 50, 30),
78+
]
79+
80+
for row in rows:
81+
ws.append(row)
82+
83+
chart1 = BarChart()
84+
chart1.type = "col"
85+
chart1.style = 10
86+
chart1.title = "销量柱状图"
87+
chart1.y_axis.title = '销量'
88+
chart1.x_axis.title = '月份'
89+
90+
data = Reference(ws, min_col=2, min_row=1, max_row=8, max_col=3)
91+
series = Reference(ws, min_col=1, min_row=2, max_row=8)
92+
chart1.add_data(data, titles_from_data=True)
93+
chart1.set_categories(series)
94+
ws.add_chart(chart1, "A10")
95+
96+
wb.save('barChart.xlsx')
97+
print('打开文件 barChart.xlsx 查看结果')
98+
99+
def pieChart():
100+
from openpyxl import Workbook
101+
from openpyxl.chart import PieChart, Reference
102+
103+
data = [
104+
['水果', '销量'],
105+
['苹果', 50],
106+
['樱桃', 30],
107+
['橘子', 10],
108+
['香蕉', 40],
109+
]
110+
111+
wb = Workbook()
112+
ws = wb.active
113+
114+
for row in data:
115+
ws.append(row)
116+
117+
pie = PieChart()
118+
labels = Reference(ws, min_col=1, min_row=2, max_row=5)
119+
data = Reference(ws, min_col=2, min_row=1, max_row=5)
120+
pie.add_data(data, titles_from_data=True)
121+
pie.set_categories(labels)
122+
pie.title = "水果销量占比"
123+
124+
ws.add_chart(pie, "D1")
125+
wb.save('piechart.xlsx')
126+
print('打开文件 piechart.xlsx 查看结果')
127+
128+
if __name__ == '__main__':
129+
createWorkbook()
130+
loadWorkbook()
131+
operateCell()
132+
setCellFormat()
133+
barChart()
134+
pieChart()

yeke/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
+ [py-mouse](https://github.com/JustDoPython/python-examples/tree/master/yeke/py-mouse) :用 Python 画一只福鼠
44
+ [py-anjia](https://github.com/JustDoPython/python-examples/tree/master/yeke/py-anjia) :用 Python 了解一下安家
55
+ [py-qrcode](https://github.com/JustDoPython/python-examples/tree/master/yeke/py-qrcode) :用 Python 生成炫酷二维码及解析
6-
6+
+ [py-recogLP](https://github.com/JustDoPython/python-examples/tree/master/yeke/py-recogLP) :如何用 Python 识别车牌
77

88
---
99

0 commit comments

Comments
 (0)