自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

十個(gè)有趣的 Python 高級(jí)腳本,建議收藏!

開發(fā) 后端
今天,小F就帶大家學(xué)習(xí)一下10個(gè)Python腳本程序。雖然簡(jiǎn)單,不過還是蠻有用的。

在日常的工作中,我們總會(huì)面臨到各式各樣的問題。

其中不少的問題,使用一些簡(jiǎn)單的Python代碼就能解決。

比如不久前的復(fù)旦大佬,用130行Python代碼硬核搞定核酸統(tǒng)計(jì),大大提升了效率,節(jié)省了不少時(shí)間。

今天,小F就帶大家學(xué)習(xí)一下10個(gè)Python腳本程序。

雖然簡(jiǎn)單,不過還是蠻有用的。

有興趣的可以自己去實(shí)現(xiàn),找到對(duì)自己有幫助的技巧。

1、Jpg轉(zhuǎn)Png

圖片格式轉(zhuǎn)換,以前小F可能第一時(shí)間想到的是【格式工廠】這個(gè)軟件。

如今編寫一個(gè)Python腳本就能完成各種圖片格式的轉(zhuǎn)換,此處以jpg轉(zhuǎn)成png為例。

有兩種解決方法,都分享給大家。

# 圖片格式轉(zhuǎn)換, Jpg轉(zhuǎn)Png
# 方法①
from PIL import Image
img = Image.open('test.jpg')
img.save('test1.png')
# 方法②
from cv2 import imread, imwrite
image = imread("test.jpg", 1)
imwrite("test2.png", image)

2、PDF加密和解密

如果你有100個(gè)或更多的PDF文件需要加密,手動(dòng)進(jìn)行加密肯定是不可行的,極其浪費(fèi)時(shí)間。

使用Python的pikepdf模塊,即可對(duì)文件進(jìn)行加密,寫一個(gè)循環(huán)就能進(jìn)行批量加密文檔。

# PDF加密
import pikepdf
pdf = pikepdf.open("test.pdf")
pdf.save('encrypt.pdf', encryption=pikepdf.Encryption(owner="your_password", user="your_password", R=4))
pdf.close()

有加密那么便會(huì)有解密,代碼如下。

# PDF解密
import pikepdf
pdf = pikepdf.open("encrypt.pdf", password='your_password')
pdf.save("decrypt.pdf")
pdf.close()

3、獲取電腦的配置信息

很多小伙伴可能會(huì)使用魯大師來(lái)看自己的電腦配置,這樣還需要下載一個(gè)軟件。

使用Python的WMI模塊,便可以輕松查看你的電腦信息。

# 獲取計(jì)算機(jī)信息
import wmi
def System_spec():
Pc = wmi.WMI()
os_info = Pc.Win32_OperatingSystem()[0]
processor = Pc.Win32_Processor()[0]
Gpu = Pc.Win32_VideoController()[0]
os_name = os_info.Name.encode('utf-8').split(b'|')[0]
ram = float(os_info.TotalVisibleMemorySize) / 1048576
print(f'操作系統(tǒng): {os_name}')
print(f'CPU: {processor.Name}')
print(f'內(nèi)存: {ram} GB')

print(f'顯卡: {Gpu.Name}')
print("\n計(jì)算機(jī)信息如上 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑")
System_spec()

就以小F自己的電腦為例,運(yùn)行代碼就能看到配置。

4、解壓文件

使用zipfile模塊進(jìn)行文件解壓,同理也可以對(duì)文件進(jìn)行壓縮。

# 解壓文件
from zipfile import ZipFile
unzip = ZipFile("file.zip", "r")
unzip.extractall("output Folder")

5、Excel工作表合并

幫助你將Excel工作表合并到一張表上,表內(nèi)容如下圖。

6張表,其余表的內(nèi)容和第一張表都一樣。

設(shè)置表格數(shù)量為5,將會(huì)合并前5張表的內(nèi)容。

import pandas as pd
# 文件名
filename = "test.xlsx"
# 表格數(shù)量
T_sheets = 5
df = []
for i in range(1, T_sheets+1):
sheet_data = pd.read_excel(filename, sheet_name=i, header=None)
df.append(sheet_data)
# 合并表格
output = "merged.xlsx"
df = pd.concat(df)
df.to_excel(output)

結(jié)果如下。

6、將圖像轉(zhuǎn)換為素描圖

和之前的圖片格式轉(zhuǎn)換有點(diǎn)類似,就是對(duì)圖像進(jìn)行處理。

以前大家可能會(huì)使用到美圖秀秀,現(xiàn)在可能就是抖音的濾鏡了。

其實(shí)使用Python的OpenCV,就能夠快速實(shí)現(xiàn)很多你想要的效果。

# 圖像轉(zhuǎn)換
import cv2
# 讀取圖片
img = cv2.imread("img.jpg")
# 灰度
grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
invert = cv2.bitwise_not(grey)
# 高斯濾波
blur_img = cv2.GaussianBlur(invert, (7, 7), 0)
inverse_blur = cv2.bitwise_not(blur_img)
sketch_img = cv2.divide(grey, inverse_blur, scale=256.0)
# 保存
cv2.imwrite('sketch.jpg', sketch_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

原圖如下。

素描圖如下,還挺好看的。

7、獲取CPU溫度

有了這個(gè)Python腳本,你將不需要任何軟件來(lái)了解CPU的溫度。

# 獲取CPU溫度
from time import sleep
from pyspectator.processor import Cpu
cpu = Cpu(monitoring_latency=1)
with cpu:
while True:
print(f'Temp: {cpu.temperature} °C')
sleep(2)

8、提取PDF表格

有的時(shí)候,我們需要從PDF中提取表格數(shù)據(jù)。

一時(shí)間你可能會(huì)先想到手工整理,但是當(dāng)工作量特別大,手工可能就比較費(fèi)勁。

然后你可能會(huì)想到一些軟件和網(wǎng)絡(luò)工具來(lái)提取 PDF 表格。

下面這個(gè)簡(jiǎn)單的腳本將幫助你在一秒鐘內(nèi)完成相同的操作。

# 方法①
import camelot
tables = camelot.read_pdf("tables.pdf")
print(tables)
tables.export("extracted.csv", f="csv", compress=True)
# 方法②, 需要安裝Java8
import tabula
tabula.read_pdf("tables.pdf", pages="all")
tabula.convert_into("table.pdf", "output.csv", output_format="csv", pages="all")

PDF文檔的內(nèi)容如下,包含了一個(gè)表格。

提取到的CSV文件內(nèi)容如下。

9、截圖

該腳本將簡(jiǎn)單地截取屏幕截圖,而無(wú)需使用任何屏幕截圖軟件。

在下面的代碼中,給大家展示了兩種Python截取屏幕截圖的方法。

# 方法①
from mss import mss
with mss() as screenshot:
screenshot.shot(output='scr.png')
# 方法②
import PIL.ImageGrab
scr = PIL.ImageGrab.grab()
scr.save("scr.png")

10、拼寫檢查器

這個(gè)Python腳本可以進(jìn)行拼寫檢查,當(dāng)然只對(duì)英文有效,畢竟中文博大精深吶。

# 拼寫檢查
# 方法①
import textblob
text = "mussage"
print("original text: " + str(text))
checked = textblob.TextBlob(text)
print("corrected text: " + str(checked.correct()))
# 方法②
import autocorrect
spell = autocorrect.Speller(lang='en')
# 以英語(yǔ)為例
print(spell('cmputr'))
print(spell('watr'))
print(spell('survice'))
責(zé)任編輯:龐桂玉 來(lái)源: Python編程時(shí)光
相關(guān)推薦

2022-08-29 14:56:56

Python腳本代碼

2024-04-11 09:13:17

設(shè)計(jì)模式開發(fā)

2015-07-15 13:54:13

2020-06-14 14:51:27

Java數(shù)據(jù)開發(fā)

2012-04-28 09:28:43

MySQL數(shù)據(jù)庫(kù)數(shù)據(jù)庫(kù)優(yōu)化

2024-06-21 10:46:44

2010-11-04 10:33:33

職場(chǎng)

2022-04-20 10:43:24

Linux命令

2022-10-10 23:19:02

Python腳本語(yǔ)言工具庫(kù)

2011-07-05 18:34:54

SEO

2023-07-02 14:21:06

PythonMatplotlib數(shù)據(jù)可視化庫(kù)

2022-05-04 20:51:28

API設(shè)計(jì)高性能

2023-12-22 15:32:20

2024-12-27 08:14:28

2022-11-07 16:06:15

TypeScript開發(fā)技巧

2019-11-28 15:30:46

收藏微軟PPT

2023-04-28 19:44:31

shell腳本命令

2021-03-11 09:02:37

SQL數(shù)據(jù)庫(kù)數(shù)據(jù)

2024-08-14 14:42:00

2024-09-14 09:26:17

Python網(wǎng)絡(luò)編程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)