10月18日学习总结
一、办公自动化:Excel文件读取
from datetime import datetime
import openpyxl
from openpyxl.cell.cell import Cell
from openpyxl.chart import Reference, LineChart
from openpyxl.styles import Alignment, Font, Border, Side
from openpyxl.worksheet.worksheet import Worksheet
def display(value, delim=' '):
if isinstance(value, float):
print(f'{value:>10.2f}', end=delim) # 右对齐,保留10位宽度
elif isinstance(value, datetime):
print(value.strftime('%Y年%m月%d日'), end=delim)
elif isinstance(value, int):
print(f'{value:>10d}', end=delim)
else:
print(value, end=delim)
1. 获取工作簿
workbook = openpyxl.load_workbook('resources/阿里巴巴2020年股票数据.xlsx')
2. 获取工作表
# sheet = workbook.active
# 获取所有的工作表workbook.worksheets
for sheet in workbook.worksheets:
print(sheet.title)
# 通过下标获取工作表
sheet = workbook.worksheets[0]
print(sheet.title)
# 通过工作表名获取工作表(如果没有提示,可以在后面注释变量类型)
sheet = workbook['股票数据']
3. 获取工作表的属性
print(sheet.max_row, sheet.max_column)
print(sheet.dimensions)
4. 操作单元格
cell = sheet['E5'] # type: Cell
# cell = sheet.cell(5, 5)
print(cell.value)
# 修改单元格内容
cell.value = 200
sheet['E5'] = 200
sheet.cell(5, 5, 200)
5. 遍历循环所有单元格
for row in range(1, sheet.max_row + 1):
for col in range(1, sheet.max_column + 1):
display(sheet.cell(row, col).value)
print()
6. 公式计算
sheet['E256'] = '=average(e2:e255)'
sheet['F256'] = '=sum(f2:f255)'
7. 设置格式
cell = sheet['E256']
cell.value = '我爱你中国'
# 对齐方式
cell.alignment = Alignment(horizontal='center', vertical='center')
# 字体
cell.font = Font(name='微软雅黑', size=20, color='330099')
# 边框
side = Side(color='ff6600', style='mediumDashed')
cell.border = Border(left=side, right=side, top=side, bottom=side)
# 行高和列宽
sheet.row_dimensions[256].height = 50
sheet.column_dimensions['E'].width = 35
8. 绘制图表
c1 = LineChart()
c1.title = '一月份收盘价和开盘价'
c1.style = 13
c1.y_axis.title = '价格'
c1.x_axis.title = '日期'
# 添加数据引用(纵轴)
data = Reference(sheet, min_col=4, min_row=1, max_row=23)
c1.add_data(data, titles_from_data=True)
# 设置数据类别(横轴)
cats = Reference(sheet, min_col=1, min_row=2, max_row=23)
c1.set_categories(cats)
# 给数据序列设置样式
s1 = c1.series[0]
s1.marker.symbol = "triangle"
s1.marker.graphicalProperties.solidFill = "ff0000"
s1.marker.graphicalProperties.line.solidFill = "0000ff"
s1.smooth = True
# 将图表添加到工作表中
sheet.add_chart(c1, "A258")
9. 保存工作簿
workbook.save('resources/阿里巴巴2020年股票数据.xlsx')
二. Python发送邮件
创建SMTT_SSL对象
1.登录授权
2.发送邮件
3.结束会话
# 发送文本
import smtplib
from email.mime.text import MIMEText
# 创建SMTP_SSL对象
smtp_obj = smtplib.SMTP_SSL(host='smtp.163.com',port=465)
# 1.登录授权
smtp_obj.login('wby5135124@163.com','YYKOSUNNXKELSUVP')
content="""天气预报说:明天有暴雨。所以如果明天A计划失败,咱们就执行B计划"""
mime_text = MIMEText(content,'plain','utf-8')
mime_text['From'] = 'wby5135124@163.com'
mime_text['To'] = '1735651388@qq.com'
mime_text['Subject'] = '秘密行动'
# 2.发送邮件
smtp_obj.sendmail(
from_addr='wby5135124@163.com',
to_addrs=['1735651388@qq.com'],
msg=mime_text.as_string()
)
# 3.结束会话
smtp_obj.quit()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bmBscOUo-1634656720330)(C:\Users\wby\AppData\Roaming\Typora\typora-user-images\image-20211019231449282.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gB2NnF4M-1634656720333)(C:\Users\wby\AppData\Roaming\Typora\typora-user-images\image-20211018151722992.png)]
# 发送文本加附件(pdf和excel)
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# 创建SMTP_SSL对象
smtp_obj = smtplib.SMTP_SSL(host='smtp.163.com', port=465)
# 1.登录授权
smtp_obj.login('wby5135124@163.com', 'YYKOSUNNXKELSUVP')
m_part = MIMEMultipart()
m_part['From'] = 'wby5135124@163.com'
m_part['To'] = '1735651388@qq.com'
m_part['Subject'] = '秘密行动123'
content = """天气预报说:明天有暴雨。所以如果明天B计划失败,咱们就执行C计划"""
mime_text = MIMEText(content, 'plain', 'utf-8')
m_part.attach(mime_text)
with open('resources/第29课:用Python发送邮件和短信.pdf', 'rb') as file:
pdf_file = MIMEText(file.read(), 'base64', 'utf-8')
pdf_file['content-type'] = 'application/pdf' # 设置内容类型
pdf_file['content-disposition'] = 'attachment; filename="Python-email-sms.pdf"'
m_part.attach(pdf_file)
with open('resources/阿里巴巴2020年股票数据.xlsx', 'rb') as file:
excel_file = MIMEText(file.read(), 'base64', 'utf-8')
excel_file['content-type'] = 'application/vnd.ms-excel'
excel_file['content-disposition'] = 'attachment; filename="alibaba-stock.xlsx"'
m_part.attach(excel_file)
# 2.发送邮件
smtp_obj.sendmail(
from_addr='wby5135124@163.com',
to_addrs=['1735651388@qq.com'],
msg=m_part.as_string()
)
# 3.结束会话
smtp_obj.quit()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xgFxvbta-1634656720336)(C:\Users\wby\AppData\Roaming\Typora\typora-user-images\image-20211019231619014.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oDHI4ZOj-1634656720339)(C:\Users\wby\AppData\Roaming\Typora\typora-user-images\image-20211019231630816.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tTmm1hSf-1634656720341)(C:\Users\wby\AppData\Roaming\Typora\typora-user-images\image-20211019231644077.png)]
om’,
to_addrs=[‘1735651388@qq.com’],
msg=m_part.as_string()
)
3.结束会话
smtp_obj.quit()
[外链图片转存中...(img-xgFxvbta-1634656720336)]
[外链图片转存中...(img-oDHI4ZOj-1634656720339)]
[外链图片转存中...(img-tTmm1hSf-1634656720341)]