1.对PPT的操作
在终端中,下载操作PPT文件的工具库:
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple python-pptx
1.创建PPT
import pptx
p = pptx.Presentation() # 生成ppt对象
# 选择布局
layout = p.slide_layouts[1]# 共提供了9种布局0-8
slide = p.slides.add_slide(layout)
p.save('test1.ppt')
2.段落的使用
import pptx
# 生成ppt对象
p = pptx.Presentation()
# 选择布局
layout = p.slide_layouts[1]
slide = p.slides.add_slide(layout)
#段落的使用
title = slide.placeholders[0]
title.text = '题目'
placeholder = slide.placeholders[1]
placeholder.text = 'Python办公自动化操作PPT\n使用场景非常多'
p.save('test2.ppt')
3.创建多个段落
import pptx
from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT
from pptx.util import Pt
# 生成ppt对象
p = pptx.Presentation()
# 选择布局
layout = p.slide_layouts[1]
slide = p.slides.add_slide(layout)
#段落的使用
title = slide.placeholders[0]
title.text = '题目'
placeholder = slide.placeholders[1]
paragraph1 = placeholder.text_frame.add_paragraph()
paragraph1.text = 'Java语言通常用于构建工程'
paragraph1.bold = True
paragraph1.font.italic = True
paragraph1.font.size = Pt(16)
paragraph1.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER
paragraph2 = placeholder.text_frame.add_paragraph()
paragraph2.text = 'Python语言通常情况用于完成特定功能,如数据分析数据采取,办公自动化等'
paragraph2.font.size = Pt(16)
paragraph2.alignment = PP_PARAGRAPH_ALIGNMENT.RIGHT
p.save('test3.ppt')
4.自定义的段落
import pptx
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT
from pptx.util import Pt, Inches
# 生成ppt对象
p = pptx.Presentation()
# 选择布局
layout = p.slide_layouts[1]
slide = p.slides.add_slide(layout)
left = top = width = height = Inches(5)
box = slide.shapes.add_textbox(left,top,width,height)
para = box.text_frame.add_paragraph()
para.text = "这是一个自定义的段落"
para.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER
para.font.size = Pt(30)
para.font.color.rgb = RGBColor(255,0,0)
para.font.name = '微软雅黑'
p.save('test4.ppt')
5.添加表格
import pptx
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT
from pptx.util import Pt, Inches
# 生成ppt对象
p = pptx.Presentation()
# 选择布局
layout = p.slide_layouts[1]
slide = p.slides.add_slide(layout)
rows = 10
cols = 2
left = top =Inches(2)
width = Inches(6)
height =Inches(1)
table = slide.shapes.add_table(rows,cols,left,top,width,height).table
for index,_ in enumerate(range(rows)):
for sub_index in range(cols):
table.cell(index,sub_index).text = '%s:%s' % (index,sub_index)
p.save('test5.ppt')
6.添加图片
import pptx
from pptx.util import Inches
# 生成ppt对象
p = pptx.Presentation()
# 选择布局
layout = p.slide_layouts[1]
slide = p.slides.add_slide(layout)
slide.shapes.add_picture(image_file='1.jpg',width=Inches(6),height=Inches(6),left=Inches(1),top=Inches(1))
p.save('test6.ppt')
7.读取PPT
import pptx
p = pptx.Presentation('test3.ppt')
for slide in p.slides:
for shape in slide.shapes:
if shape.has_text_frame:
print(shape.text_frame.text)
if shape.has_table:
for cell in shape.table.iter_cells():
print(cell.text)
2.对邮箱的操作
1.获取授权码
以QQ邮箱为例
2.发送普通邮件
在终端中,下载发送邮件的工具库:
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple schedule
1.发送一个基础邮件
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
mail_host = 'smtp.qq.com' # smtp.163.com
mail_user = '1234567890@qq.com'
mail_pass = 'khamipuqfyabcdef' # 不是登录邮箱的密码,是授权码
sender = '1234567890@qq.com' # 发送者邮箱
receivers = ['1234567890@qq.com'] #接收者邮箱
message = MIMEText('这是一个测试邮件','plain','utf-8') #发送内容
message['From'] = Header(sender) # 配置发送者
message['Subject'] = Header('Python办公自动化邮件操作','utf-8')
try:
smtpobj = smtplib.SMTP_SSL(mail_host,465)
# smtpobj = smtpobj.SMTP() #连接163
# smtpobj.connect(mail_host,25) # 连接163
smtpobj.login(mail_user,mail_pass)
smtpobj.sendmail(sender,receivers,message.as_string())
except smtplib.SMTPException as e:
print('error :%s'%e)
2.发送一个html
# message = MIMEText('这是一个测试邮件','plain','utf-8')👇
message = MIMEText('<p style="color:red">这是一个段落</p>','html','utf-8')
3.发送带附件的邮件
# message = MIMEText('这是一个测试邮件','plain','utf-8')
# message = MIMEText('<p style="color:red">这是一个段落</p>','html','utf-8')
message = MIMEMultipart()# 发送带附件的邮件👈
#附件内容与配置
attr = MIMEText(open('1.jpg','rb').read(),'base64','utf-8') #发送文件,已base64的格式对文件进行操作
attr['Content-Type'] = 'application/octet-stream' # 设置请求类型
attr['Content-Disposition'] = 'attachment;filename="1.jpg"'
message.attach(attr) #将配置好的附件参数传递至发送信息函数中
message.attach(MIMEText('这是一个带附件的邮件','plain','utf-8'))# 发送文本信息
3.定时发送邮件
import smtplib
import time
from email.mime.text import MIMEText
from email.header import Header
import schedule
mail_host = 'smtp.qq.com' # smtp.163.com
mail_user = '1234567890@qq.com'
mail_pass = 'khamipuqfyabcdef' # 不是登录邮箱的密码,是授权码
sender = '1234567890@qq.com' # 发送者邮箱
receivers = ['1234567890@qq.com'] #接收者邮箱
message = MIMEText('这是一个测试邮件', 'plain', 'utf-8') # 发送内容
message['From'] = Header(sender) # 配置发送者
message['Subject'] = Header('Python办公自动化邮件操作','utf-8')
def send():
try:
smtpobj = smtplib.SMTP_SSL(mail_host, 465)
# smtpobj = smtpobj.SMTP() #连接163
# smtpobj.connect(mail_host,25) # 连接163
smtpobj.login(mail_user, mail_pass)
smtpobj.sendmail(sender, receivers, message.as_string())
except smtplib.SMTPException as e:
print('error :%s' % e)
if __name__ == '__main__':
schedule.every(10).seconds.do(send) #每10秒调用send函数发送数据
while 1:
schedule.run_pending() # 执行
time.sleep(1)