当前位置:   article > 正文

Python这些操作,逆天且实用!

pyshorteners python3.11

15d5a8538c054b83ec66f9190e97215b.png

Hello, 大家好,我是菜鸟哥。

是不是经常遇到这种窘境?当亲戚朋友来家做客,问起WiFi密码,然后翻箱倒柜、问了一圈也找不到。

今天,给大家介绍Python一些鲜为人知的操作。

这些操作,并非是炫技,而是真的实用!

1. 显示WiFi密码

我们经常忘记wifi的密码,可是每当家里来了亲戚朋友问起WiFi密码,却又无从下手。

这里有一个技巧,我们可以列出所有的设备和它们的密码。

  1. import subprocess #import required library
  2. data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8').split('\n') #store profiles data in "data" variable
  3. profiles = [i.split(":")[1][1:-1] for i in data if"All User Profile"in i] #store the profile by converting them to list
  4. for i in profiles:
  5. # running the command to check passwords
  6. results = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', i, 'key=clear']).decode('utf-8').split('\n')
  7. # storing passwords after converting them to list
  8. results = [b.split(":")[1][1:-1] for b in results if"Key Content"in b]
  9. try:
  10. print ("{:<30}| {:<}".format(i, results[0]))
  11. except IndexError:
  12. print ("{:<30}| {:<}".format(i, ""))

2. 视频转GIF

近年来,GIF出现了热潮。大多数流行的社交媒体平台,都为用户提供了各种GIF,以更有意义和更容易理解的方式表达他们的想法。

很多同学为了将视频转成GIF可谓是煞费苦心,而且在这个过程中踩了不少坑。

而使用Python,简短的几行代码即可解决!

安装

pip install moviepy

代码

  1. from moviepy.editor import VideoFileClip
  2. clip = VideoFileClip("video_file.mp4") # Enter your video's path
  3. clip.write_gif("gif_file.gif", fps = 10)

3. 桌面提醒

当我们在做项目或其他事情的时候,我们可能会忘记某些重要的事情,我们可以通过在系统上看到一个简单的通知来记住这些。

在python的帮助下,我们可以创建个性化的通知,并可以将其安排在特定的时间。

安装

pip install win10toast, schedule

代码

  1. import win10toast
  2. toaster = win10toast.ToastNotifier()
  3. import schedule
  4. import time
  5. def job():
  6. toaster.show_toast('提醒', "到吃饭时间了!", duration = 15)
  7. schedule.every().hour.do(job) #scheduling for every hour; you can even change the scheduled time with schedule library
  8. whileTrue:
  9. schedule.run_pending()
  10. time.sleep(1)

4. 自定义快捷键

有时,我们在工作中需要频繁地输入一些单词。如果我们能使我们的键盘自动化,只用缩写就能写出这些经常使用的单词,这不是很有趣吗?

没错,我们可以用Python使之成为可能。

安装

pip install keyboard

代码

  1. import keyboard
  2. #press sb and space immediately(otherwise the trick wont work)
  3. keyboard.add_abbreviation('ex', '我是一条测试数据!') #provide abbreviation and the original word here
  4. # Block forever, like `while True`.
  5. keyboard.wait()

然后,在任何位置输入ex加空格就可以快速补全对应的语句!

5. 文本转PDF

我们都知道,部分笔记和在线可用的书籍都是以pdf的形式存在。

这是因为pdf可以以同样的方式存储内容,而不用考虑平台或设备。

因此,如果我们有文本文件,我们可以在python库fpdf的帮助下将它们转换成PDF文件。

安装

pip install fpdf

代码

  1. from fpdf import FPDF
  2. pdf = FPDF()
  3. pdf.add_page() # Add a page
  4. pdf.set_font("Arial", size = 15) # set style and size of font
  5. f = open("game_notes.txt", "r") # open the text file in read mode
  6. # insert the texts in pdf
  7. for x in f:
  8. pdf.cell(50,5, txt = x, ln = 1, align = 'C')
  9. #pdf.output("path where you want to store pdf file\\file_name.pdf")
  10. pdf.output("game_notes.pdf")

6. 生成二维码

我们在日常生活中经常看到二维码,QR码节省了很多用户的时间。

我们也可以用python库qrcode为网站或个人资料创建独特的QR码。

安装

pip install qrcode

代码

  1. #import the library
  2. import qrcode
  3. #link to the website
  4. input_data = "https://car-price-prediction-project.herokuapp.com/"
  5. #Creating object
  6. #version: defines size of image from integer(1 to 40), box_size = size of each box in pixels, border = thickness of the border.
  7. qr = qrcode.QRCode(version=1,box_size=10,border=5)
  8. #add_date : pass the input text
  9. qr.add_data(input_data)
  10. #converting into image
  11. qr.make(fit=True)
  12. #specify the foreground and background color for the img
  13. img = qr.make_image(fill='black', back_color='white')
  14. #store the image
  15. img.save('qrcode_img.png')

7. 翻译

我们生活在一个多语言的世界里。

因此,为了理解不同的语言,我们需要一个语言翻译器。

我们可以在python库Translator的帮助下创建我们自己的语言翻译器。

安装

pip install translate

代码

  1. #import the library
  2. from translate import Translator
  3. #specifying the language
  4. translator = Translator(to_lang="Hindi")
  5. #typing the message
  6. translation = translator.translate('Hello!!! Welcome to my class')
  7. #print the translated message
  8. print(translation)

8. Google搜索

有时候编程太忙碌,以至于我们觉得懒得打开浏览器来搜索我们想要的答案。

但是有了google这个神奇的python库,我们只需要写3行代码就可以搜索我们的查询,而不需要手动打开浏览器并在上面搜索我们的查询。

安装

pip install google

代码

  1. #import library
  2. from googlesearch import search
  3. #write your query
  4. query = "best course for python"
  5. # displaying 10 results from the search
  6. for i in search(query, tld="co.in", num=10, stop=10, pause=2):
  7. print(i)
  8. #you will notice the 10 search results(website links) in the output.

9. 提取音频

在某些情况下,我们有mp4文件,但我们只需要其中的音频,比如用另一个视频的音频制作一个视频。

我们为获得相同的音频文件做了足够的努力,但我们失败了。

这个问题用python库moviepy可以轻而易举的解决。

安装

pip install moviepy

代码

  1. #import library
  2. import moviepy.editor as mp
  3. #specify the mp4 file here(mention the file path if it is in different directory)
  4. clip = mp.VideoFileClip('video.mp4')
  5. #specify the name for mp3 extracted
  6. clip.audio.write_audiofile('Audio.mp3')
  7. #you will notice mp3 file will be created at the specified location.

10. 生成短链接

经常和各种各样的链接打交道,过长的URL让思绪混乱不堪!

于是,就有了各种各样的短链接生成工具。

不过,大多数使用都比较麻烦。

我们可以在python库pyshorteners的帮助下创建我们自己的短链接生成器。

安装

pip install pyshorteners

代码

  1. #import library
  2. import pyshorteners
  3. #creating object
  4. s=pyshorteners.Shortener()
  5. #type the url
  6. url = "type the youtube link here"
  7. #print the shortend url
  8. print(s.tinyurl.short(url))

读到这里,会发现,Python除了完成工作中涉及到的机器学习、数据分析等项目开发,还可以完成很多非常 有趣,且能够极大提高工作效率的操作。

本文就是抛砖引玉一下,希望大家能够寻找到更多有趣的Python玩法!


这是我开发的机器人公众号小号,目前增加了天气查询,955公司名单,关注时间查询;后面还会增加图片功能和每日送书抽奖送书活动,以及调戏功能,欢迎来体验,捧场。

全新机器人公众号上线啦,欢迎调戏!

 
 
  1. 推荐阅读:
  2. 入门: 最全的零基础学Python的问题  | 零基础学了8个月的Python  | 实战项目 |学Python就是这条捷径
  3. 干货:爬取豆瓣短评,电影《后来的我们》 | 38年NBA最佳球员分析 |   从万众期待到口碑扑街!唐探3令人失望  | 笑看新倚天屠龙记 | 灯谜答题王 |用Python做个海量小姐姐素描图 |碟中谍这么火,我用机器学习做个迷你推荐系统电影
  4. 趣味:弹球游戏  | 九宫格  | 漂亮的花 | 两百行Python《天天酷跑》游戏!
  5. AI: 会做诗的机器人 | 给图片上色 | 预测收入 | 碟中谍这么火,我用机器学习做个迷你推荐系统电影
  6. 小工具: Pdf转Word,轻松搞定表格和水印! | 一键把html网页保存为pdf!|  再见PDF提取收费! | 用90行代码打造最强PDF转换器,word、PPT、excel、markdown、html一键转换 | 制作一款钉钉低价机票提示器! |60行代码做了一个语音壁纸切换器天天看小姐姐!|

年度爆款文案

点阅读原文,看B站我的视频!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/805666
推荐阅读
相关标签
  

闽ICP备14008679号