是不是经常遇到这种窘境?当亲戚朋友来家做客,问起 WiFi 密码,然后翻箱倒柜、问了一圈也找不到。
今天,给大家介绍 Python 一些鲜为人知的操作。
这些操作,并非是炫技,而是真的实用!
1. 显示 WiFi 密码
我们经常忘记 wifi 的密码,可是每当家里来了亲戚朋友问起 WiFi 密码,却又无从下手。
这里有一个技巧,我们可以列出所有的设备和它们的密码。
import subprocess #import required library
data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8').split('\n') #store profiles data in "data" variable
profiles = [i.split(":")[1][1:-1] for i in data if"All User Profile"in i] #store the profile by converting them to list
for i in profiles:
# running the command to check passwords
results = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', i, 'key=clear']).decode('utf-8').split('\n')
# storing passwords after converting them to list
results = [b.split(":")[1][1:-1] for b in results if"Key Content"in b]
try:
print ("{:<30}| {:<}".format(i, results[0]))
except IndexError:
print ("{:<30}| {:<}".format(i, ""))
2. 视频转 GIF
近年来,GIF 出现了热潮。大多数流行的社交媒体平台,都为用户提供了各种 GIF,以更有意义和更容易理解的方式表达他们的想法。
很多同学为了将视频转成 GIF 可谓是煞费苦心,而且在这个过程中踩了不少坑。
而使用 Python,简短的几行代码即可解决!
安装
pip install moviepy
代码
from moviepy.editor import VideoFileClip
clip = VideoFileClip("video_file.mp4") # Enter your video's path
clip.write_gif("gif_file.gif", fps = 10)
3. 桌面提醒
当我们在做项目或其他事情的时候,我们可能会忘记某些重要的事情,我们可以通过在系统上看到一个简单的通知来记住这些。
在 python 的帮助下,我们可以创建个性化的通知,并可以将其安排在特定的时间。
安装
pip install win10toast, schedule
代码
import win10toast
toaster = win10toast.ToastNotifier()
import schedule
import time
def job():
toaster.show_toast('提醒', "到吃饭时间了!", duration = 15)
schedule.every().hour.do(job) #scheduling for every hour; you can even change the scheduled time with schedule library
whileTrue:
schedule.run_pending()
time.sleep(1)
4. 自定义快捷键
有时,我们在工作中需要频繁地输入一些单词。如果我们能使我们的键盘自动化,只用缩写就能写出这些经常使用的单词,这不是很有趣吗?
没错,我们可以用 Python 使之成为可能。
安装
pip install keyboard
代码
import keyboard
#press sb and space immediately(otherwise the trick wont work)
keyboard.add_abbreviation('ex', '我是一条测试数据!') #provide abbreviation and the original word here
# Block forever, like `while True`.
keyboard.wait()
然后,在任何位置输入 ex 加空格就可以快速补全对应的语句!
5. 文本转 PDF
我们都知道,部分笔记和在线可用的书籍都是以 pdf 的形式存在。
这是因为 pdf 可以以同样的方式存储内容,而不用考虑平台或设备。
因此,如果我们有文本文件,我们可以在 Python 库 fpdf 的帮助下将它们转换成 PDF 文件。
安装
pip install fpdf
代码
from fpdf import FPDF
pdf = FPDF()
pdf.add_page() # Add a page
pdf.set_font("Arial", size = 15) # set style and size of font
f = open("game_notes.txt", "r") # open the text file in read mode
# insert the texts in pdf
for x in f:
pdf.cell(50,5, txt = x, ln = 1, align = 'C')
#pdf.output("path where you want to store pdf file\file_name.pdf")
pdf.output("game_notes.pdf")
6. 生成二维码
我们在日常生活中经常看到二维码,QR 码节省了很多用户的时间。
我们也可以用 Python 库 qrcode 为网站或个人资料创建独特的 QR 码。
安装
pip install qrcode
代码
#import the library
import qrcode
#link to the website
input_data = "https://car-price-prediction-project.herokuapp.com/"
#Creating object
#version: defines size of image from integer(1 to 40), box_size = size of each box in pixels, border = thickness of the border.
qr = qrcode.QRCode(version=1,box_size=10,border=5)
#add_date : pass the input text
qr.add_data(input_data)
#converting into image
qr.make(fit=True)
#specify the foreground and background color for the img
img = qr.make_image(fill='black', back_color='white')
#store the image
img.save('qrcode_img.png')
7. 翻译
我们生活在一个多语言的世界里。
因此,为了理解不同的语言,我们需要一个语言翻译器。
我们可以在 Python 库 Translator 的帮助下创建我们自己的语言翻译器。
安装
pip install translate
代码
#import the library
from translate import Translator
#specifying the language
translator = Translator(to_lang="Hindi")
#typing the message
translation = translator.translate('Hello!!! Welcome to my class')
#print the translated message
print(translation)
8. Google 搜索
有时候编程太忙碌,以至于我们觉得懒得打开浏览器来搜索我们想要的答案。
但是有了 Google 这个神奇的 Python 库,我们只需要写 3 行代码就可以搜索我们的查询,而不需要手动打开浏览器并在上面搜索我们的查询。
安装
pip install google
代码
#import library
from googlesearch import search
#write your query
query = "best course for python"
# displaying 10 results from the search
for i in search(query, tld="co.in", num=10, stop=10, pause=2):
print(i)
#you will notice the 10 search results(website links) in the output.
9. 提取音频
在某些情况下,我们有 mp4 文件,但我们只需要其中的音频,比如用另一个视频的音频制作一个视频。
我们为获得相同的音频文件做了足够的努力,但我们失败了。
这个问题用 Python 库 moviepy 可以轻而易举的解决。
安装
pip install moviepy
代码
#import library
import moviepy.editor as mp
#specify the mp4 file here(mention the file path if it is in different directory)
clip = mp.VideoFileClip('video.mp4')
#specify the name for mp3 extracted
clip.audio.write_audiofile('Audio.mp3')
#you will notice mp3 file will be created at the specified location.
10. 生成短链接
经常和各种各样的链接打交道,过长的 URL 让思绪混乱不堪!
于是,就有了各种各样的短链接生成工具。
不过,大多数使用都比较麻烦。
我们可以在 Python 库 pyshorteners 的帮助下创建我们自己的短链接生成器。
安装
pip install pyshorteners
代码
#import library
import pyshorteners
#creating object
s=pyshorteners.Shortener()
#type the url
url = "type the youtube link here"
#print the shortend url
print(s.tinyurl.short(url))
读到这里,会发现,Python 除了完成工作中涉及到的机器学习、数据分析等项目开发,还可以完成很多非常 有趣,且能够极大提高工作效率的操作。
本文就是抛砖引玉一下,希望大家能够寻找到更多有趣的 Python 玩法!
Python 的迅速崛起对整个行业来说都是极其有利的 ,但“人红是非多
”,导致它平添了许许多多的批评,不过依旧挡不住它火爆的发展势头。
如果你对Python感兴趣,想要学习python,这里给大家分享一份Python全套学习资料,都是我自己学习时整理的,希望可以帮到你,一起加油!
😝有需要的小伙伴,可以点击下方链接免费领取或者V扫描下方二维码免费领取🆓
Python全套学习资料
1️⃣零基础入门
① 学习路线
对于从来没有接触过Python的同学,我们帮你准备了详细的学习成长路线图。可以说是最科学最系统的学习路线,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
② 路线对应学习视频
还有很多适合0基础入门的学习视频,有了这些视频,轻轻松松上手Python~
③练习题
每节视频课后,都有对应的练习题哦,可以检验学习成果哈哈!
2️⃣国内外Python书籍、文档
① 文档和书籍资料
3️⃣Python工具包+项目源码合集
①Python工具包
学习Python常用的开发软件都在这里了!每个都有详细的安装教程,保证你可以安装成功哦!
②Python实战案例
光学理论是没用的,要学会跟着一起敲代码,动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。100+实战案例源码等你来拿!
③Python小游戏源码
如果觉得上面的实战案例有点枯燥,可以试试自己用Python编写小游戏,让你的学习过程中增添一点趣味!
4️⃣Python面试题
我们学会了Python之后,有了技能就可以出去找工作啦!下面这些面试题是都来自阿里、腾讯、字节等一线互联网大厂,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
5️⃣Python兼职渠道
而且学会Python以后,还可以在各大兼职平台接单赚钱,各种兼职渠道+兼职注意事项+如何和客户沟通,我都整理成文档了。
上述所有资料 ⚡️ ,朋友们如果有需要的,可以扫描下方👇👇👇二维码免费领取🆓