您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 昌吉分类信息网,免费分类信息发布

Python办公自动化十大场景,你都知道吗?

2024/3/12 2:24:06发布18次查看
在编程世界里,python已经是名副其实的网红了。曾经一个学汉语言的研究生,问我怎么学python,因为他们课程论文里需要用到文本分析,用python来跑数据。我和他说,你看两天语法,就可以上手开干,不会的再查资料。后来这位同学半个月就用python把论文数据搞好了。
所以python最大优势在于容易学,门槛比java、c++低非常多,给非程序员群体提供了用代码干活的可能性。当然python能成为大众编程工具,不光光是因为易学,还因为python有成千上万的工具包,遍布各行各业。
举10几个大众办公常见的例子,python都能高效处理。
1、python处理excel数据可以使用pandas、xlwings、openpyxl等包来对excel进行增删改查、格式调整等操作,甚至可以使用python函数来对excel数据进行分析。
读取excel表格:
import xlwings as xw wb = xw.book()# this will create a new workbook wb = xw.book('filename.xlsx')# connect to a file that is open or in the current working directory wb = xw.book(r'c:pathtofile.xlsx')# on windows: use raw strings to escape backslashes
将matplotlib绘图写入excel表格:
import matplotlib.pyplot as plt import xlwings as xw fig = plt.figure() plt.plot([1, 2, 3]) sheet = xw.book().sheets[0] sheet.pictures.add(fig, name='myplot', update=true)
2、python处理pdf文本pdf几乎是最常见的文本格式,很多人有各种处理pdf的需求,比如制作pdf、获取文本、获取图片、获取表格等。python中有pypdf、pdfplumber、reportlab、pymupdf等包可以轻松实现这些需求。
提取pdf文字:
import pypdf2 pdffile = open('example.pdf','rb') pdfreader = pypdf2.pdffilereader(pdffile) print(pdfreader.numpages) page = pdfreader.getpage(0) print(page.extracttext()) pdffile.close()
提取pdf表格:
# 提取pdf表格 import pdfplumber with pdfplumber.open(example.pdf) as pdf: page01 = pdf.pages[0] #指定页码 table1 = page01.extract_table()#提取单个表格 # table2 = page01.extract_tables()#提取多个表格 print(table1)
3、python处理email在python中可以使用smtplib配合email库,来实现邮件的自动化传输,非常方便。
import smtplib import email # 负责将多个对象集合起来 from email.mime.multipart import mimemultipart from email.header import header # smtp服务器,这里使用163邮箱 mail_host = smtp.163.com # 发件人邮箱 mail_sender = ******@163.com # 邮箱授权码,注意这里不是邮箱密码,如何获取邮箱授权码,请看本文最后教程 mail_license = ******** # 收件人邮箱,可以为多个收件人 mail_receivers = [******@qq.com,******@outlook.com] mm = mimemultipart('related') # 邮件正文内容 body_content = 你好,这是一个测试邮件! # 构造文本,参数1:正文内容,参数2:文本格式,参数3:编码方式 message_text = mimetext(body_content,plain,utf-8) # 向mimemultipart对象中添加文本对象 mm.attach(message_text) # 创建smtp对象 stp = smtplib.smtp() # 设置发件人邮箱的域名和端口,端口地址为25 stp.connect(mail_host, 25) # set_debuglevel(1)可以打印出和smtp服务器交互的所有信息 stp.set_debuglevel(1) # 登录邮箱,传递参数1:邮箱地址,参数2:邮箱授权码 stp.login(mail_sender,mail_license) # 发送邮件,传递参数1:发件人邮箱地址,参数2:收件人邮箱地址,参数3:把邮件内容格式改为str stp.sendmail(mail_sender, mail_receivers, mm.as_string()) print(邮件发送成功) # 关闭smtp对象 stp.quit()
4、python处理数据库数据库是我们常用的办公应用,python中有各种数据库驱动接口包,支持对数据库的增删改查、运维管理工作。比如说pymysql包对应mysql、psycopg2包对应postgresql、pymssql包对应sqlserver、cxoracle包对应oracle、pymongo包对应mongodb等等。
对mysql的连接查询
import pymysql # 打开数据库连接 db = pymysql.connect(host='localhost', user='testuser', password='test123', database='testdb') # 使用 cursor() 方法创建一个游标对象 cursor cursor = db.cursor() # 使用 execute()方法执行 sql 查询 cursor.execute(select version()) # 使用 fetchone() 方法获取单条数据. data = cursor.fetchone() print (database version : %s % data) # 关闭数据库连接 db.close()
5、python处理批量文件对很多办公场景来说,批量处理文件一直是个脏活累活,python可以帮你脱离苦海。python中有很多处理系统文件的包,比如sys、os、shutil、glob、path.py等等。
批量删除不同文件夹下的同名文件夹:
import os,shutil import sys import numpy as np def arrange_file(dir_path0): for dirpath,dirnames,filenames in os.walk(dir_path0): if 'my_result' in dirpath: # print(dirpath) shutil.rmtree(dirpath)
批量修改文件后缀名:
import os def file_rename(): path = input(请输入你需要修改的目录(格式如'f:\test'):) old_suffix = input('请输入你需要修改的后缀(需要加点.):') new_suffix = input('请输入你要改成的后缀(需要加点.):') file_list = os.listdir(path) for file in file_list: old_dir = os.path.join(path, file) print('当前文件:', file) if os.path.isdir(old_dir): continue if old_suffix != os.path.splitext(file)[1]: continue filename = os.path.splitext(file)[0] new_dir = os.path.join(path, filename + new_suffix) os.rename(old_dir, new_dir) if __name__ == '__main__': file_rename()
6、python控制鼠标这是很多人的需求,实现对鼠标的自动控制,去做一些流水线的工作,比如软件测试。
python有个pyautogui库可以任意地去控制你的鼠标。
控制鼠标左击/右击/双击函数以及测试源码:
# 获取鼠标位置 import pyautogui as pg try: while true: x, y = pg.position() print(str(x) + + str(y))#输出鼠标位置 if 1746 < x < 1800 and 2 < y < 33: pg.click()#左键单击 if 1200 < x < 1270 and 600 < y < 620: pg.click(button='right')#右键单击 if 1646 < x < 1700 and 2 < y < 33: pg.doubleclick()#左键双击 except keyboardinterrupt: print(n)
7、python控制键盘同样的,python也可以通过pyautogui控制键盘。
键盘写入:
import pyautogui #typewrite()无法输入中文内容,中英文混合的只能输入英文 #interval设置文本输入速度,默认值为0 pyautogui.typewrite('你好,world!',interval=0.5)
8、python压缩文件压缩文件是办公中常见的操作,一般压缩会使用压缩软件,需要手动操作。
python中有很多包支持文件压缩,可以让你自动化压缩或者解压缩本地文件,或者将内存中的分析结果进行打包。比如zipfile、zlib、tarfile等可以实现对.zip、.rar、.7z等压缩文件格式的操作。
压缩文件:
import zipfile try: with zipfile.zipfile(c://test.zip,mode=w) as f: f.write(c://test.txt)#写入压缩文件,会把压缩文件中的原有覆盖 except exception as e: print(异常对象的类型是:%s%type(e)) print(异常对象的内容是:%s%e) finally: f.close()
解压文件:
import zipfile try: with zipfile.zipfile(c://test.zip,mode=a) as f: f.extractall(c://,pwd=broot) ##将文件解压到指定目录,解压密码为root except exception as e: print(异常对象的类型是:%s%type(e)) print(异常对象的内容是:%s%e) finally: f.close()
9、python爬取网络数据python爬虫应该是最受欢迎的功能,也是广大python爱好者们入坑的主要的原因。
python中有非常多的包支持爬虫,而爬虫包又分为抓取、解析两种。
比如说requests、urllib这种是网络数据请求工具,也就是抓取包;xpath、re、bs4这种会对抓取下来的网页内容进行解析,称为解析包。
爬取百度首页图片,并保存到本地:
# 导入urlopen from urllib.request import urlopen # 导入beautifulsoup from bs4 import beautifulsoup as bf # 导入urlretrieve函数,用于下载图片 from urllib.request import urlretrieve # 请求获取html html = urlopen(http://www.baidu.com/) # 用beautifulsoup解析html obj = bf(html.read(),'html.parser') # 从标签head、title里提取标题 title = obj.head.title # 只提取logo图片的信息 logo_pic_info = obj.find_all('img',class_=index-logo-src) # 提取logo图片的链接 logo_url = https:+logo_pic_info[0]['src'] # 使用urlretrieve下载图片 urlretrieve(logo_url, 'logo.png')
10、python处理图片图表图片处理、图表可视化涉及到图像处理,这也是python的强项,现在诸如图像识别、计算机视觉等前沿领域也都会用到python。
在python中处理图像的包有scikit image、pil、opencv等,处理图表的包有matplotlib、plotly、seaborn等。
对图片进行黑白化处理:
from pil import image from pil import imageenhance img_main = image.open(u'e:/login1.png') img_main = img_main.convert('l') threshold1 = 138 table1 = [] for i in range(256): if i < threshold1: table1.append(0) else: table1.append(1) img_main = img_main.point(table1, 1) img_main.save(u'e:/login3.png')
生成统计图表:
import numpy as np import matplotlib.pyplot as plt n = 5 menmeans = (20, 35, 30, 35, 27) womenmeans = (25, 32, 34, 20, 25) menstd = (2, 3, 4, 1, 2) womenstd = (3, 5, 2, 3, 3) ind = np.arange(n)# the x locations for the groups width = 0.35 # the width of the bars: can also be len(x) sequence p1 = plt.bar(ind, menmeans, width, yerr=menstd) p2 = plt.bar(ind, womenmeans, width, bottom=menmeans, yerr=womenstd) plt.ylabel('scores') plt.title('scores by group and gender') plt.xticks(ind, ('g1', 'g2', 'g3', 'g4', 'g5')) plt.yticks(np.arange(0, 81, 10)) plt.legend((p1[0], p2[0]), ('men', 'women')) plt.show()
小结总之python会成为大众化的编程语言,帮助到更多需要的人。
以上就是python办公自动化十大场景,你都知道吗?的详细内容。
昌吉分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录