Hello,大家好,我是陈程!

大家平时有没有留神到你每天可能会执行许多的反复的工作,例如浏览 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()

最初的话

本文分享的4个实用的python自动化脚本,你感觉有帮忙的话,就点个赞,感激你的反对!