Python的自动化脚本有哪些
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,Python的自动化脚本有哪些,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。前言:大家平时有没有注意到你每天可能会执行许
千家信息网最后更新 2024年09月22日Python的自动化脚本有哪些1、将 PDF 转换为音频文件
2、从列表中播放随机音乐
3、不再有书签了
4、清理下载文件夹
Python的自动化脚本有哪些,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
前言:
大家平时有没有注意到你每天可能会执行许多的重复的任务,例如阅读 pdf、播放音乐、打开书签、清理文件夹等等。
我将分享4个实用的python的自动化脚本,无需手动一次又一次地完成这些任务,非常方便。
1、将 PDF 转换为音频文件
脚本可以将 pdf 转换为音频文件,原理也很简单,首先用 PyPDF 提取 pdf 中的文本,然后用 Pyttsx3 将文本转语音。关于文本转语音,你还可以看这篇文章。
FastAPI:快速开发一个文本转语言的接口。
代码如下:
import pyttsx3,PyPDF2pdfreader = PyPDF2.PdfFileReader(open('story.pdf','rb'))speaker = pyttsx3.init()for page_num in range(pdfreader.numPages): text = pdfreader.getPage(page_num).extractText() ## extracting text from the PDF cleaned_text = text.strip().replace('\n',' ') ## Removes unnecessary spaces and break lines print(cleaned_text) ## Print the text from PDF #speaker.say(cleaned_text) ## Let The Speaker Speak The Text speaker.save_to_file(cleaned_text,'story.mp3') ## Saving Text In a audio file 'story.mp3' speaker.runAndWait()speaker.stop()
2、从列表中播放随机音乐
这个脚本会从歌曲文件夹中随机选择一首歌进行播放,需要注意的是 os.startfile
仅支持 Windows
系统。
import random, osmusic_dir = 'G:\new english songs'songs = os.listdir(music_dir)song = random.randint(0,len(songs))print(songs[song]) ## Prints The Song Nameos.startfile(os.path.join(music_dir, songs[0]))
3、不再有书签了
每天睡觉前,我都会在网上搜索一些好内容,第二天可以阅读。大多数时候,我把遇到的网站或文章添加为书签,但我的书签每天都在增加,以至于现在我的浏览器周围有100多个书签。因此,在python
的帮助下,我想出了另一种方法来解决这个问题。现在,我把这些网站的链接复制粘贴到文本文件中,每天早上我都会运行脚本,在我的浏览器中再次打开所有这些网站。
import webbrowserwith open('./websites.txt') as reader: for link in reader: webbrowser.open(link.strip())
代码用到了 webbrowser
,是 Python 中的一个库,可以自动在默认浏览器中打开 URL。
4、清理下载文件夹
世界上最混乱的事情之一是开发人员的下载文件夹,里面存放了很多杂乱无章的文件,此脚本将根据大小限制来清理您的下载文件夹,
有限清理比较旧的文件:
import osimport threadingimport time def get_file_list(file_path):#文件按最后修改时间排序 dir_list = os.listdir(file_path) if not dir_list: return else: dir_list = sorted(dir_list, key=lambda x: os.path.getmtime(os.path.join(file_path, x))) return dir_list def get_size(file_path): " " "[summary] Args: file_path ([type]): [目录] Returns: [type]: 返回目录大小,MB " " " totalsize=0 for filename in os.listdir(file_path): totalsize=totalsize+os.path.getsize(os.path.join(file_path, filename)) #print(totalsize / 1024 / 1024) return totalsize / 1024 / 1024 def detect_file_size(file_path, size_Max, size_Del): " " "[summary] Args: file_path ([type]): [文件目录] size_Max ([type]): [文件夹最大大小] size_Del ([type]): [超过size_Max时要删除的大小] " " " print(get_size(file_path)) if get_size(file_path) > size_Max: fileList = get_file_list(file_path) for i in range(len(fileList)): if get_size(file_path) > (size_Max - size_Del): print ("del :%d %s" % (i + 1, fileList[i])) #os.remove(file_path + fileList[i]) def detectFileSize(): #检测线程,每个5秒检测一次 while True: print('======detect============') detect_file_size("/Users/aaron/Downloads/", 100, 30) time.sleep(5) if __name__ == "__main__": #创建检测线程 detect_thread = threading.Thread(target = detectFileSize) detect_thread.start()
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。
文件
脚本
文件夹
书签
文本
大小
浏览器
目录
网站
帮助
检测
浏览
自动化
代码
任务
内容
文章
线程
语音
音乐
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器惠普msa20哪年生产的
魔兽 任务 数据库
银行开放网络安全吗
数据库中创建用户名和密码
网络安全监管工作存在的问题
c 开发mdb数据库查询
自学的软件开发公司会要吗
奇迹世界全球服务器玩哪个国家
网络安全行业的认识和理解
台式服务器是什么意思
asp.net部署到服务器
ds软件开发
海港区网络安全检查工作
抖音第三方数据库
网络技术发展的重要
服务器柜子散热风扇异响
怎么样学好软件开发
上海网络技术转让市场价格
华为云备份服务器异常3911
数组元素遍历添加到数据库
数据库间传输
登录学信网怎么用万方数据库查重
使用cmcc网络安全吗
新手可以做手机软件开发吗
梦幻西游服务器端
数据库中的逻辑如何设计
淄博多乐网络技术服务有限公司
服务器安全日志很多登录
宝鱼软件开发室
安徽软件开发学习