办公自动化是指利用计算机和软件技术来自动化、半自动化办公中的工作流程和任务,提高工作效率。Python是一种通用的编程语言,有丰富的库和模块,可以用来实现办公自动化。以下是20个用Python实现办公自动化的例子和代码:
自动发送电子邮件
import smtplib
from email.mime.text import MIMEText
def send_email(subject, body, to):
from_addr = 'your_email_address'
password = 'your_email_password'
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = to
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(from_addr, password)
server.sendmail(from_addr, to, msg.as_string())
server.quit()
send_email('Test Subject', 'Test Body', '[email protected]')
自动填写表格
import openpyxl
wb = openpyxl.load_workbook('example.xlsx')
sheet = wb.active
sheet['A1'] = 'Name'
sheet['B1'] = 'Age'
sheet['C1'] = 'Gender'
sheet['A2'] = 'John'
sheet['B2'] = 30
sheet['C2'] = 'Male'
wb.save('example.xlsx')
自动从网站获取数据
import requests
from bs4 import BeautifulSoup
url = 'https://www.example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.title.string
print(title)
自动备份文件
import shutil
shutil.copy('source_file.txt', 'destination_folder/')
自动创建文件夹
import os
os.mkdir('new_folder')
自动处理PDF文件
import PyPDF2
with open('example.pdf', 'rb') as pdf_file:
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
num_pages = pdf_reader.numPages
print('Number of Pages:', num_pages)
自动处理文本文件
with open('example.txt', 'r') as txt_file:
lines = txt_file.readlines()
for line in lines:
print(line)
自动处理CSV文件
import csv
with open('example.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
for row in csv_reader:
print(row)
自动处理JSON文件
import json
with open('example.json', 'r') as json_file:
data = json.load(json_file)
print(data)
自动处理XML文件
import xml.etree.ElementTree as ET
tree = ET.parse('example.xml')
root = tree.getroot()
for child in root:
print(child.tag, child.attrib)
自动下载文件
import urllib.request
url = 'https://www.example.com/file.txt'
urllib.request.urlretrieve(url, 'file.txt')
自动截图
import pyautogui
pyautogui.screenshot('screenshot.png')
自动调整图片大小
from PIL import Image
image = Image.open('example.jpg')
new_image = image.resize((400, 400))
new_image.save('resized_image.jpg')
自动制作图表
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Example Chart')
plt.show()
自动处理日期和时间
import datetime
current_time = datetime.datetime.now()
print(current_time.strftime('%Y-%m-%d %H:%M:%S'))
自动处理密码
import getpass
password = getpass.getpass('Enter your password:')
print('Your password is:', password)
自动处理压缩文件
import zipfile
with zipfile.ZipFile('example.zip', 'r') as zip_file:
zip_file.extractall('destination_folder')
自动处理Excel文件
import pandas as pd
data = {'Name': ['John', 'Mary', 'Peter'], 'Age': [30, 25, 35]}
df = pd.DataFrame(data)
df.to_excel('example.xlsx', index=False)
自动处理数据库
import sqlite3
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
cursor.execute('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)')
cursor.execute('INSERT INTO users (name, age) VALUES (?, ?)', ('John', 30))
conn.commit()
conn.close()
自动处理网络数据
import socket
HOST = 'www.example.com'
PORT = 80
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n')
data = s.recv(1024)
print(data.decode())
以上是20个用Python实现办公自动化的例子和代码,希望对你有所帮助。