Hello ~ Hello everyone, I'm Madoka
When we work ~ often do some repetitive things
For example: reading news, sending emails, checking the weather, cleaning folders, etc.
Is there any way to shorten or even cancel the time to do these things?
Of course there is! ! That is to use an automated script to pull~
This saves us having to do these tasks manually over and over again
Today I will bring you 8 python automation scripts to improve work efficiency~
1. Automatically read web news
This script can grab text from a web page and automatically read it aloud
This is a great option when you want to hear the news.
The code is divided into two parts, the first is to crawl the text of the web page through the crawler, and the second is to read the text aloud through the reading tool.
Required third-party libraries:
- Beautiful Soup
Classic HTML/XML text parser, used to extract web page information;
- requests
It is easy to use the anti-sky HTTP tool, which is used to send requests to web pages to obtain data;
- Pyttsx3
Convert text to speech and control rate, frequency and speech;
The specific code is as follows:
python study Exchange Q Group: 770699889 ### import pyttsx3 import requests from bs4 import BeautifulSoup voices = engine.getProperty('voices') newVoiceRate = 130 ## Reduce The Speech Rate engine.setProperty('rate',newVoiceRate) engine.setProperty('voice', voices[1].id) def speak(audio): engine.say(audio) engine.runAndWait() text = str(input("Paste article\n")) res = requests.get(text) articles = [] for i in range(len(soup.select('.p'))): article = soup.select('.p')[i].getText().strip() articles.append(article) text = " ".join(articles) speak(text) # engine.save_to_file(text, 'test.mp3') ## If you want to save the speech as a audio file engine.runAndWait()
2. Automated data exploration
Data exploration is the first step in a data science project, you need to understand the basic information of the data to further analyze deeper value.
Generally, we will use pandas, matplotlib and other tools to explore the data
But you need to write a lot of code yourself, if you want to improve efficiency, Dtale is a good choice.
Dtale features an automated analysis report with one line of code, which combines the Flask backend and the React frontend
Provides us with an easy way to view and analyze Pandas data structures.
We can use Dtale on Jupyter.
Required third-party libraries:
- Dtale
Automatically generate analysis reports
The specific code is as follows:
### Importing Seaborn Library For Some Datasets import seaborn as sns ### Printing Inbuilt Datasets of Seaborn Library print(sns.get_dataset_names()) ### Loading Titanic Dataset df=sns.load_dataset('titanic') ### Importing The Library import dtale
3. Automatically send multiple emails
This script can help us send emails in batches at regular intervals, and the content and attachments of emails can also be customized and adjusted, which is very practical.
Compared with mail clients, the advantage of Python scripts is that they can deploy mail services intelligently, in batches, and with high customization.
Required third-party libraries:
for managing email messages;
- Smtlib
Send email to an SMTP server, which defines an SMTP client session object that can send mail to any computer with an SMTP or ESMTP listener on the Internet
- Pandas
Tools for data analysis and cleaning;
The specific code is as follows:
import smtplib from email.message import EmailMessage def send_email(remail, rsubject, rcontent): email = EmailMessage() ## Creating a object for EmailMessage email['from'] = 'The Pythoneer Here' ## Person who is sending email['to'] = remail ## Whom we are sending email['subject'] = rsubject ## Subject of email email.set_content(rcontent) ## content of email with smtplib.SMTP(host='smtp.gmail.com',port=587)as smtp: smtp.ehlo() ## server object smtp.starttls() ## used to send data between server and client smtp.login("deltadelta371@gmail.com","delta@371") ## login id and password of gmail smtp.send_message(email) ## Sending email print("email send to ",remail) ## Printing success message if __name__ == '__main__': df = pd.read_excel('list.xlsx') length = len(df)+1 for index, item in df.iterrows(): email = item[0] subject = item[1] content = item[2]
4. Play random music from the list
This script will randomly select a song from the songs folder to play,
Note that os.startfile
Only Windows systems are supported.
The specific code is as follows:
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]))
5. Smart Weather Information
The National Weather Service website provides an API for obtaining weather forecasts, which directly returns weather data in json format.
So just take out the corresponding fields from json.
The following is the URL of the weather of the designated city (county, district), open the URL directly, and the weather data of the corresponding city will be returned.
for example:
http://www.weather.com.cn/data/cityinfo/101021200.html The weather website corresponding to Xuhui District, Shanghai.
The specific code is as follows:
mport requests import json import logging as log def get_weather_wind(url): r = requests.get(url) if r.status_code != 200: log.error("Can't get weather data!") info = json.loads(r.content.decode()) # get wind data data = info['weatherinfo'] WD = data['WD'] WS = data['WS'] return "{}({})".format(WD, WS) def get_weather_city(url): # open url and get return data r = requests.get(url) if r.status_code != 200: log.error("Can't get weather data!") # convert string to json info = json.loads(r.content.decode()) # get useful data data = info['weatherinfo'] city = data['city'] temp1 = data['temp1'] temp2 = data['temp2'] weather = data['weather'] return "{} {} {}~{}".format(city, weather, temp1, temp2) if __name__ == '__main__': msg = """**weather reminder**: {} {} {} {} source: National Weather Service """.format( get_weather_city('http://www.weather.com.cn/data/cityinfo/101021200.html'), get_weather_wind('http://www.weather.com.cn/data/sk/101021200.html'), get_weather_city('http://www.weather.com.cn/data/cityinfo/101020900.html'), get_weather_wind('http://www.weather.com.cn/data/sk/101020900.html') ) print(msg)
6. Convert PDF to Audio File
Script to convert pdf to audio file
The principle is also very simple, first use PyPDF to extract the text in the pdf, and then use Pyttsx3 to convert the text to speech.
The specific code is as follows:
python study Exchange Q Group: 770699889 ### 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()
7. Convert long URLs to short URLs
Sometimes those big URLs become very annoying and hard to read and share, this script can turn long URLs into short ones.
The specific code is as follows:
import contextlib from urllib.parse import urlencode from urllib.request import urlopen import sys def make_tiny(url): request_url = ('http://tinyurl.com/api-create.php?' + urlencode({'url':url})) with contextlib.closing(urlopen(request_url)) as response: return response.read().decode('utf-8') def main(): for tinyurl in map(make_tiny, sys.argv[1:]): print(tinyurl) if __name__ == '__main__': main()
8. Clean up the Downloads folder
One of the most confusing things in the world is the developer's download folder,
There are a lot of messy files inside.
This script will clean up your downloads folder according to the size limit,
Limited cleanup of older files.
The specific code is as follows:
import os import threading import time def get_file_list(file_path): #Files sorted by 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]): [Table of contents] python study Exchange Q Group: 770699889 ### Returns: [type]: returns the 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]): [Folder maximum size] size_Del ([type]): [Exceed size_Max size to delete] """ 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])
Well, today's sharing ends here~
If you have any questions about the article, or have other questions about python, you can leave a message in the comment area or privately message me.
If you think the articles I share are good, you can follow me, or like the articles (/≧▽≦)/