Have you noticed that you may perform many repetitive tasks every day , For example, reading pdf, Play music , Open bookmark , Clean up folders, etc .

today , I will share 4 A practical python Automated scripts for , There is no need to manually complete these tasks over and over again , Very convenient .

1, take PDF Convert to audio file

Scripts can pdf Convert to audio file , The principle is also very simple , First use PyPDF extract pdf Text in , Then use Pyttsx3
Convert text to speech . About text transfer , You can also read this article .

FastAPI: Rapid development of a text to language interface .

The code is as follows :

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

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



2, Play random music from the list

This script will randomly select a song from the song folder to play , It should be noted that os.startfile Only supported Windows system .

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, There are no more bookmarks

Before going to bed every day , I will search some good content on the Internet , You can read it the next day . Most of the time , I bookmark websites or articles I encounter , But my bookmarks are increasing every day , So now there are around my browser 100 Multiple bookmarks . therefore , stay python With the help of , I came up with another way to solve the problem . Now? , I copy and paste the links of these websites into a text file , I run the script every morning , Open all these sites again in my browser .

import webbrowser

with open('./websites.txt') as reader:

for link in reader:


Code used webbrowser, yes Python A library in , It can be opened automatically in the default browser URL.

4, Clean up download folders

One of the most confusing things in the world is the developer's download folder , There are a lot of disorderly documents in it , This script will clean up your download folder based on the size limit , Limited cleanup of older files :

import os

import threading

import time

def get_file_list(file_path):

# The files are sorted by the time of last modification

dir_list = os.listdir(file_path)

if not dir_list:



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]


file_path ([type]): [ catalogue ]


[type]: Return directory size ,MB

" " "


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]


file_path ([type]): [ File directory ]

size_Max ([type]): [ Maximum folder size ]

size_Del ([type]): [ exceed size_Max Size to delete when ]

" " "


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():

# Detection thread , every other 5 Once per second

while True:


detect_file_size("/Users/aaron/Downloads/", 100, 30)


if __name__ == "__main__":

# Create detection thread

detect_thread = threading.Thread(target = detectFileSize)


Finally, I wish you progress every day !! study Python The most important thing is mentality . We are bound to encounter many difficulties in the process of learning , Maybe you can't solve it if you want to break your head . This is normal , Don't rush to deny yourself , Doubt yourself . If you encounter difficulties at the beginning of your study , Looking for one python Learning and communication environment , You can join us , Receive learning materials , Discuss together , It will save a lot of time , Reduce many difficulties encountered .