Hello, everyone, I am Chi Ye!
Have you noticed how many repetitive tasks you perform every day, such as reading PDFS, playing music, opening bookmarks, clearing folders, etc.
Today, I’m going to share four useful Python automation scripts that are very convenient without having to do these tasks manually over and over again.
1. Convert PDF files to audio files
Scripts can convert PDF files to audio files. The principle is very simple, first using PyPDF to extract the text in PDF, then using Pyttsx3 to convert the text to speech. You can also read this article on text-to-speech.
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()
Copy the code
2. Play random music from the list
This script will play a random song from the songs folder. Note that os.startfile only supports Windows.
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]))
Copy the code
3. No more bookmarks
Every day before I go to bed, I search the Internet for something good to read the next day. Most of the time, I bookmark websites or articles I come across, but my bookmarks are growing every day, so much so that I now have more than 100 bookmarks around my browser. So, with the help of Python, I came up with another way to solve this problem. Now, I copy and paste links to these sites into text files, and every morning I run a script to open all of them again in my browser.
import webbrowser
with open('./websites.txt') as reader:
for link in reader:
webbrowser.open(link.strip())
Copy the code
The code uses WebBrowser, a Python library that automatically opens urls in the default browser.
4. Clean up the download folder
One of the most cluttered things in the world is the developer’s download folder, which contains a lot of messy files. This script will clean up your download folder according to the size limit, limited cleanup of older files:
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]): [directory] Returns: [type]: Returns 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]): [Size to remove when size_Max exceeds] "" "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(): # check threads every 5 seconds 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 ()Copy the code
The last word
This article shares 4 useful Python automation scripts that you found helpful. Please click the “like” button. Thanks for your support!