欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

4個的Python自動化腳本分享

 更新時間:2021年12月20日 17:15:22   作者:遲業(yè)  
這篇文章主要給大家分享了4個的Python自動化腳本,自動化腳本無需手動一次又一次地完成這些任務(wù),非常方便,下面我能就一起進入文章來來了解實現(xiàn)吧,希望對你有所幫助

前言:

大家平時有沒有注意到你每天可能會執(zhí)行許多的重復(fù)的任務(wù),例如閱讀 pdf、播放音樂、打開書簽、清理文件夾等等。
我將分享4個實用的python的自動化腳本,無需手動一次又一次地完成這些任務(wù),非常方便。

1、將 PDF 轉(zhuǎn)換為音頻文件

腳本可以將 pdf 轉(zhuǎn)換為音頻文件,原理也很簡單,首先用 PyPDF 提取 pdf 中的文本,然后用 Pyttsx3 將文本轉(zhuǎn)語音。關(guān)于文本轉(zhuǎn)語音,你還可以看這篇文章。

FastAPI:快速開發(fā)一個文本轉(zhuǎn)語言的接口。

代碼如下:

import pyttsx3,PyPDF2
pdfreader = 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 系統(tǒng)。

import random, os
music_dir = 'G:\new english songs'
songs = os.listdir(music_dir)
song = random.randint(0,len(songs))
print(songs[song])  ## Prints The Song Name
os.startfile(os.path.join(music_dir, songs[0])) 


3、不再有書簽了

每天睡覺前,我都會在網(wǎng)上搜索一些好內(nèi)容,第二天可以閱讀。大多數(shù)時候,我把遇到的網(wǎng)站或文章添加為書簽,但我的書簽每天都在增加,以至于現(xiàn)在我的瀏覽器周圍有100多個書簽。因此,在python的幫助下,我想出了另一種方法來解決這個問題?,F(xiàn)在,我把這些網(wǎng)站的鏈接復(fù)制粘貼到文本文件中,每天早上我都會運行腳本,在我的瀏覽器中再次打開所有這些網(wǎng)站。

import webbrowser
with open('./websites.txt') as reader:
    for link in reader:
        webbrowser.open(link.strip())


代碼用到了 webbrowser,是 Python 中的一個庫,可以自動在默認瀏覽器中打開 URL。

4、清理下載文件夾

世界上最混亂的事情之一是開發(fā)人員的下載文件夾,里面存放了很多雜亂無章的文件,此腳本將根據(jù)大小限制來清理您的下載文件夾,

有限清理比較舊的文件:

import os
import threading
import 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__":
    #創(chuàng)建檢測線程
    detect_thread = threading.Thread(target = detectFileSize)
    detect_thread.start()

到此這篇關(guān)于4個的Python自動化腳本分享的文章就介紹到這了,更多相關(guān)Python自動化腳本分享內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論