网站上线注意问题,wordpress导航添加双语菜单,织梦网站变成手机站,做设计排版除了昵图网还有什么网站目录
一、Python 自动化办公的准备工作
1.1 安装必要的库
1.2 设置邮件服务
二、邮件自动化处理
2.1 发送邮件
示例代码
注意事项
2.2 接收和读取邮件
示例代码
三、Excel 自动化处理
3.1 读取和写入 Excel 文件
示例代码
3.2 数据处理和分析
示例代码
四、综合…目录
一、Python 自动化办公的准备工作
1.1 安装必要的库
1.2 设置邮件服务
二、邮件自动化处理
2.1 发送邮件
示例代码
注意事项
2.2 接收和读取邮件
示例代码
三、Excel 自动化处理
3.1 读取和写入 Excel 文件
示例代码
3.2 数据处理和分析
示例代码
四、综合实例从邮件中读取 Excel 附件并分析
示例代码 随着技术的进步Python 的高效性和易用性使其成为办公自动化的强大工具。通过 Python我们可以自动处理日常工作中的邮件、Excel 表格等任务从而大幅提升效率。本文将详细介绍如何使用 Python 实现这些自动化功能并附上关键代码示例。 一、Python 自动化办公的准备工作
1.1 安装必要的库
在实现自动化办公之前需要安装相关库。以下是常用的 Python 库
邮件自动化smtplib发送邮件imaplib接收邮件email处理邮件内容。Excel 操作openpyxl操作 Excel 文件pandas数据处理。环境配置dotenv管理环境变量。
安装方式如下
pip install openpyxl pandas python-dotenv1.2 设置邮件服务
为了能够发送和接收邮件需要
确保邮箱已开启 SMTP发送和 IMAP接收服务。使用支持授权的 App 密钥如 Gmail 的“应用专用密码”。 二、邮件自动化处理
2.1 发送邮件
示例代码
以下代码实现了通过 SMTP 发送邮件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipartdef send_email(sender_email, sender_password, recipient_email, subject, body):# 创建邮件对象msg MIMEMultipart()msg[From] sender_emailmsg[To] recipient_emailmsg[Subject] subjectmsg.attach(MIMEText(body, plain))# 连接到 SMTP 服务器并发送邮件try:with smtplib.SMTP(smtp.gmail.com, 587) as server:server.starttls()server.login(sender_email, sender_password)server.send_message(msg)print(邮件发送成功)except Exception as e:print(f邮件发送失败{e})# 调用示例
send_email(sender_emailyour_emailgmail.com,sender_passwordyour_app_password,recipient_emailrecipientexample.com,subject测试邮件,body这是一封通过 Python 发送的测试邮件。
)注意事项
Gmail 用户需开启 “允许不安全应用访问” 或生成 App 密码。替换 SMTP 服务地址时其他邮箱服务商可能需要不同配置 QQ 邮箱smtp.qq.comOutlooksmtp.office365.com 2.2 接收和读取邮件
示例代码
以下代码展示如何通过 IMAP 读取未读邮件
import imaplib
import emaildef fetch_emails(email_address, password):try:# 连接 IMAP 服务器with imaplib.IMAP4_SSL(imap.gmail.com) as mail:mail.login(email_address, password)mail.select(inbox) # 选择收件箱# 搜索未读邮件status, messages mail.search(None, UNSEEN)for num in messages[0].split():status, msg_data mail.fetch(num, (RFC822))for response_part in msg_data:if isinstance(response_part, tuple):msg email.message_from_bytes(response_part[1])print(f发件人: {msg[from]})print(f主题: {msg[subject]})if msg.is_multipart():for part in msg.walk():if part.get_content_type() text/plain:print(f内容: {part.get_payload(decodeTrue).decode()})else:print(f内容: {msg.get_payload(decodeTrue).decode()})except Exception as e:print(f邮件读取失败{e})# 调用示例
fetch_emails(your_emailgmail.com, your_app_password)三、Excel 自动化处理
3.1 读取和写入 Excel 文件
示例代码
使用 openpyxl 读取和写入 Excel
import openpyxl# 打开 Excel 文件
workbook openpyxl.load_workbook(example.xlsx)
sheet workbook.active# 读取数据
for row in sheet.iter_rows(min_row1, max_row5, min_col1, max_col3):print([cell.value for cell in row])# 写入数据
sheet[A6] 新数据
workbook.save(example_updated.xlsx)
print(Excel 文件已更新)3.2 数据处理和分析
示例代码
使用 pandas 对 Excel 数据进行分析
import pandas as pd# 读取 Excel 文件
data pd.read_excel(example.xlsx)# 打印前五行数据
print(data.head())# 数据处理
data[总分] data[数学] data[英语] data[科学]
print(data)# 保存结果
data.to_excel(processed.xlsx, indexFalse)
print(数据处理完成并已保存)四、综合实例从邮件中读取 Excel 附件并分析
以下代码展示了一个完整的自动化工作流
接收邮件并提取附件。读取 Excel 数据进行分析。将结果发送回发件人。
示例代码
import os
import imaplib
import email
import pandas as pd
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplibdef fetch_and_process_email(email_address, password):# 连接 IMAPwith imaplib.IMAP4_SSL(imap.gmail.com) as mail:mail.login(email_address, password)mail.select(inbox)# 搜索含附件邮件status, messages mail.search(None, ALL)for num in messages[0].split():status, msg_data mail.fetch(num, (RFC822))for response_part in msg_data:if isinstance(response_part, tuple):msg email.message_from_bytes(response_part[1])if msg.is_multipart():for part in msg.walk():if part.get_filename(): # 找到附件file_path os.path.join(os.getcwd(), part.get_filename())with open(file_path, wb) as f:f.write(part.get_payload(decodeTrue))print(f附件已保存: {file_path})# 处理附件数据data pd.read_excel(file_path)data[总分] data.sum(axis1)processed_path processed.xlsxdata.to_excel(processed_path, indexFalse)print(数据处理完成)# 返回处理结果send_email(sender_emailemail_address,sender_passwordpassword,recipient_emailmsg[from],subject数据处理结果,body附件已处理请查看。,attachment_pathprocessed_path)def send_email(sender_email, sender_password, recipient_email, subject, body, attachment_path):msg MIMEMultipart()msg[From] sender_emailmsg[To] recipient_emailmsg[Subject] subjectmsg.attach(MIMEText(body, plain))# 添加附件with open(attachment_path, rb) as f:attachment email.mime.base.MIMEBase(application, octet-stream)attachment.set_payload(f.read())email.encoders.encode_base64(attachment)attachment.add_header(Content-Disposition, fattachment; filename{os.path.basename(attachment_path)})msg.attach(attachment)# 发送邮件with smtplib.SMTP(smtp.gmail.com, 587) as server:server.starttls()server.login(sender_email, sender_password)server.send_message(msg)print(邮件已发送)# 调用示例
fetch_and_process_email(your_emailgmail.com, your_app_password)