Hello, Hello everyone , I'm Chen Chen !
Have you noticed that you may perform many repetitive tasks every day , For example, reading pdf、 Play music 、 Open Bookmarks 、 Clean up folders, etc .
today , I will share 4 A practical python Automation script for , There is no need to manually complete these tasks again and again , Very convenient .
Scripts can pdf Convert to audio file , The principle is also simple , First use PyPDF extract pdf The text in the , And then use Pyttsx3 Convert text to voice . About text transfer , You can also read this article .
FastAPI: Quickly develop 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 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()This script will randomly select a song from the song folder to play , It should be noted that os.startfile Support only 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]))
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 that now there are around my browser 100 Multiple bookmarks . therefore , stay python With the help of the , 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:
webbrowser.open(link.strip())The code uses webbrowser, yes Python One of the Libraries , It can be opened automatically in the default browser URL.
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 according to the last modified time
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]): [ Catalog ]
Returns:
[type]: Return directory size ,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]): [ File directory ]
size_Max ([type]): [ Maximum folder size ]
size_Del ([type]): [ exceed size_Max Size to delete when ]
" " "
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():
# Detect threads , every other 5 Check once per second
while True:
print('======detect============')
detect_file_size("/Users/aaron/Downloads/", 100, 30)
time.sleep(5)
if __name__ == "__main__":
# Create detection thread
detect_thread = threading.Thread(target = detectFileSize)
detect_thread.start()This article shares 4 A practical python Automation script , If you think it helps , Just like it , Thank you for your support !
[weekly Python 100 day question brushing plan] day5~ use of built-in functions and operators
Catalog ️ Preface Topic 1 an
Python & c++ mixed call programming comprehensive practice -16c++ calls Pythons class instantiation object to access member functions and members
author : Empty bad uncle Blog