current position：Home>Share 4 practical Python automation scripts
Share 4 practical Python automation scripts
2022-02-02 07:25:33 【Late industry】
Hello, Hello everyone , I'm Chi Ye ！
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 .
1、 take PDF Convert to audio file
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 to speech , 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() Copy code
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 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)) Copy code
3、 There are no more Bookmarks
Before going to bed every day , I will search some good content on the Internet , The next day you can read . 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()) Copy code
The code uses webbrowser, yes Python One of the Libraries , It can be opened automatically in the default browser URL.
4、 Clean up the download folder
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): # Files are sorted by last modification 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 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() Copy code
This article shares 4 A practical python Automation script , If you think it helps , Just like it , Thank you for your support ！
author[Late industry],Please bring the original link to reprint, thank you.
The sidebar is recommended
- [Python] numpy notes
- 24 useful Python tips
- Pandas table beauty skills
- Python tiktok character video, CV2 module, Python implementation
- I used Python to climb my wechat friends. They are like this
- 20000 words take you into the python crawler requests library, the most complete in history!!
- Answer 2: why can you delete the table but not update the data with the same Python code
- [pandas learning notes 02] - advanced usage of data processing
- How to implement association rule algorithm? Python code and powerbi visualization are explained to you in detail (Part 2 - actual combat)
- Python adds list element append() method, extend() method and insert() method [details]
guess what you like
Introduction to Python gunicorn
Python dictionary query key value pair methods and examples
Opencv Python reads video, processes and saves it frame by frame
Python learning process and bug
Imitate the up master and realize a live broadcast room controlled by barrage with Python!
Essence! Configuration skills of 12 pandas
[Python automated operation and maintenance road] path inventory
Daily automatic health punch in (Python + Tencent cloud server)
[Python] variables, comments, basic data types
- Spring boot calls Python interface
- Using Python to make a key recorder
- Python combat case, pyGame module, python implementation routine confession artifact vs no routine confession artifact
- Python series tutorial 132 -- why use indentation syntax
- 10 minutes to learn how to play excel easily with Python
- Python develops a color dynamic two-dimensional code generator in one hour, and uses the virtual environment to package and release the EXE program.
- Elimination of grammar left recursion in Python
- Python testing - the patches in Python
- Python image processing, CV2 module, OpenCV to achieve target tracking
- How to send alarm notification to nail in Python?
- Introduction to pandas operation
- Mail sending, SMTP and exchange sending in Python 3
- Show your hand and use Python to analyze house prices
- The strongest Python visualization artifact, none of them
- 8 practical Python skills that are easy to use and don't have to suffer a loss for half a year
- Tips: teach you to generate 30 cool dynamic interactive charts with one click of pandas
- I use one line of Python code to dynamically load dependencies
- Blow up this pandas GUI artifact and automatically turn the code!
- Getting started exploring and analyzing data using Python
- Python image processing, CV2 module, OpenCV to achieve template matching
- Teach you three Python Programming Tips
- Python code reading (Chapter 69): case conversion of initial letters
- Python tutorial series 133 -- several special grammars
- Dry goods | Python operation: Python controls Excel to realize office automation
- Understand the principle of affine transformation and python implementation code
- A little trick every day, python can easily convert PDF to text and bid farewell to copy and paste
- Climb Conan barrage with Python + gephi to sort out the main plot
- As a programmer, do you know Python variable reference
- One line of code, take you to learn Python
- Summary of 22 advanced Python knowledge points, dry goods!
- [Python learning] nanny level teaching parsing and parsing XML in Python
- Use of Python JSON module
- "Python" guide to using itertools of Python standard library
- What are the functions and classifications of Python interpreters
- Python implements four schemes for timed tasks, lazy artifact
- Python can actually realize the freedom of punch in?
- What about Python memory leak? Pit filling troubleshooting tips
- Object oriented programming in Python
- Quick start - Python Basics
- [target detection (8)] a thorough understanding of the loss function of the regression box of target detection -- the principles of IOU, giou, Diou and ciou and Python code