文章目录
- import os def batch_rename_files(directory, prefix): “””批量重命名指定目录下的所有文件,添加前缀””” for filename in os.listdir(directory): new_name = f”{prefix}_{filename}” os.rename(os.path.join(directory, filename), os.path.join(directory, new_name)) print(“文件重命名完成”) # 示例:批量重命名文件 batch_rename_files(‘path/to/your/directory’, ‘new_prefix’)
- import smtplib from email.mime.text import MIMEText def send_email(to_email, subject, body): “””发送邮件””” sender = “your_email@example.com” password = “your_password” msg = MIMEText(body) msg[‘Subject’] = subject msg[‘From’] = sender msg[‘To’] = to_email with smtplib.SMTP(‘smtp.example.com’, 587) as server: server.starttls() server.login(sender, password) server.sendmail(sender, to_email, msg.as_string()) print(“邮件发送成功”) # 示例:发送邮件 send_email(‘recipient@example.com’, ‘Subject’, ‘Email body’)
- import pandas as pd def generate_excel_report(data, output_file): “””生成 Excel 报表””” df = pd.DataFrame(data) df.to_excel(output_file, index=False) print(“报表生成完成”) # 示例:生成 Excel 报表 data = {‘Name’: [‘Alice’, ‘Bob’, ‘Charlie’], ‘Age’: [25, 30, 35]} generate_excel_report(data, ‘report.xlsx’)
- import pandas as pd import os def batch_process_excel_files(directory, output_file): “””批量处理 Excel 文件,合并到一个文件中””” all_data = [] for filename in os.listdir(directory): if filename.endswith(‘.xlsx’): file_path = os.path.join(directory, filename) df = pd.read_excel(file_path) all_data.append(df) combined_df = pd.concat(all_data, ignore_index=True) combined_df.to_excel(output_file, index=False) print(“文件处理完成”) # 示例:批量处理 Excel 文件 batch_process_excel_files(‘path/to/your/directory’, ‘combined_output.xlsx’)
- from apscheduler.schedulers.blocking import BlockingScheduler import datetime def my_job(): print(“任务执行时间:”, datetime.datetime.now()) # 示例:设置定时任务 scheduler = BlockingScheduler() scheduler.add_job(my_job, ‘interval’, seconds=10) scheduler.start()
目录
- 一、自动化办公小助手的功能
- (一)批量重命名文件
- (二)发送邮件
- (三)生成 Excel 报表
- (四)批量处理 Excel 文件
- (五)定时任务
- 二、整合自动化办公小助手
- 三、总结
在日常办公中,我们常常会遇到一些重复性的任务,如批量处理文件、发送邮件、生成报表等。这些任务不仅耗时,还容易出错。今天,就让我们一起用 Python 编写一个自动化办公小助手,帮助你高效完成这些任务。
import os
def batch_rename_files(directory, prefix):
"""批量重命名指定目录下的所有文件,添加前缀"""
for filename in os.listdir(directory):
new_name = f"{prefix}_{filename}"
os.rename(os.path.join(directory, filename), os.path.join(directory, new_name))
print("文件重命名完成")
# 示例:批量重命名文件
batch_rename_files('path/to/your/directory', 'new_prefix')
import smtplib
from email.mime.text import MIMEText
def send_email(to_email, subject, body):
"""发送邮件"""
sender = "your_email@example.com"
password = "your_password"
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = to_email
with smtplib.SMTP('smtp.example.com', 587) as server:
server.starttls()
server.login(sender, password)
server.sendmail(sender, to_email, msg.as_string())
print("邮件发送成功")
# 示例:发送邮件
send_email('recipient@example.com', 'Subject', 'Email body')
import pandas as pd
def generate_excel_report(data, output_file):
"""生成 Excel 报表"""
df = pd.DataFrame(data)
df.to_excel(output_file, index=False)
print("报表生成完成")
# 示例:生成 Excel 报表
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
generate_excel_report(data, 'report.xlsx')
import pandas as pd
import os
def batch_process_excel_files(directory, output_file):
"""批量处理 Excel 文件,合并到一个文件中"""
all_data = []
for filename in os.listdir(directory):
if filename.endswith('.xlsx'):
file_path = os.path.join(directory, filename)
df = pd.read_excel(file_path)
all_data.append(df)
combined_df = pd.concat(all_data, ignore_index=True)
combined_df.to_excel(output_file, index=False)
print("文件处理完成")
# 示例:批量处理 Excel 文件
batch_process_excel_files('path/to/your/directory', 'combined_output.xlsx')
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime
def my_job():
print("任务执行时间:", datetime.datetime.now())
# 示例:设置定时任务
scheduler = BlockingScheduler()
scheduler.add_job(my_job, 'interval', seconds=10)
scheduler.start()
将上述功能整合到一个脚本中,创建一个自动化办公小助手。
import os
import smtplib
from email.mime.text import MIMEText
import pandas as pd
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime
# 批量重命名文件
def batch_rename_files(directory, prefix):
for filename in os.listdir(directory):
new_name = f"{prefix}_{filename}"
os.rename(os.path.join(directory, filename), os.path.join(directory, new_name))
print("文件重命名完成")
# 发送邮件
def send_email(to_email, subject, body):
sender = "your_email@example.com"
password = "your_password"
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = to_email
with smtplib.SMTP('smtp.example.com', 587) as server:
server.starttls()
server.login(sender, password)
server.sendmail(sender, to_email, msg.as_string())
print("邮件发送成功")
# 生成 Excel 报表
def generate_excel_report(data, output_file):
df = pd.DataFrame(data)
df.to_excel(output_file, index=False)
print("报表生成完成")
# 批量处理 Excel 文件
def batch_process_excel_files(directory, output_file):
all_data = []
for filename in os.listdir(directory):
if filename.endswith('.xlsx'):
file_path = os.path.join(directory, filename)
df = pd.read_excel(file_path)
all_data.append(df)
combined_df = pd.concat(all_data, ignore_index=True)
combined_df.to_excel(output_file, index=False)
print("文件处理完成")
# 定时任务
def my_job():
print("任务执行时间:", datetime.datetime.now())
# 主函数
def main():
# 批量重命名文件
batch_rename_files('path/to/your/directory', 'new_prefix')
# 发送邮件
send_email('recipient@example.com', 'Subject', 'Email body')
# 生成 Excel 报表
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
generate_excel_report(data, 'report.xlsx')
# 批量处理 Excel 文件
batch_process_excel_files('path/to/your/directory', 'combined_output.xlsx')
# 设置定时任务
scheduler = BlockingScheduler()
scheduler.add_job(my_job, 'interval', seconds=10)
scheduler.start()
if __name__ == "__main__":
main()
通过本文的介绍,你已经学会了如何使用 Python 编写一个自动化办公小助手,包括批量重命名文件、发送邮件、生成 Excel 报表、批量处理 Excel 文件和设置定时任务。
到此这篇关于使用Python编写一个自动化办公小助手的文章就介绍到这了,更多相关Python自动化办公内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!
您可能感兴趣的文章:
- Python自动化办公中的应用说明和脚本示例
- 办公自动化最常用的10个Python库分享
- Python自动化办公之合并多个Excel
- 10个Python自动化办公的脚本分享
- 10个Python办公自动化案例总结
- Python自动化办公之生成PDF报告详解
- Python办公自动化从Excel中计算整理数据并写入Word
- Python办公自动化处理的10大场景应用示例