收藏值得一看的10个Python高级脚本!

图片[1]-收藏值得一看的10个Python高级脚本!-山海云端论坛

1、图片格式转换:JPG转PNG

通过Python脚本实现图片格式的转换,以JPG转换为PNG为例,提供两种解决方法:

<code># 方法①:使用PIL库 from PIL import Image img = Image.open('test.jpg') img.save('test1.png') # 方法②:使用OpenCV库 from cv2 import imread, imwrite image = imread("test.jpg", 1) imwrite("test2.png", image)</code>

2、PDF加密和解密

利用pikepdf模块进行PDF文件的加密和解密操作,实现批量处理PDF文件:

<code># 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() # PDF解密 import pikepdf pdf = pikepdf.open("encrypt.pdf", password='your_password') pdf.save("decrypt.pdf") pdf.close()</code>

3、获取计算机配置信息

使用WMI模块获取计算机信息,包括操作系统、CPU、内存和显卡等:

<code>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'操作系统: {os_name}') print(f'CPU: {processor.Name}') print(f'内存: {ram} GB') print(f'显卡: {Gpu.Name}') System_spec()</code>

4、解压文件

利用zipfile模块进行文件解压操作:

<code>from zipfile import ZipFile unzip = ZipFile("file.zip", "r") unzip.extractall("output Folder")</code>

5、Excel工作表合并

使用pandas库将多个Excel工作表合并到一张表上:

<code>import pandas as pd # 文件名 filename = "test.xlsx" # 表格数量 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)</code>

6、将图像转换为素描图

使用OpenCV库将图像转换为素描图:

<code>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)</code>

7、获取CPU温度

利用pyspectator库获取CPU温度信息:

<code>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)</code>

8、提取PDF表格

使用camelot和tabula库从PDF中提取表格数据:

<code># 使用camelot import camelot tables = camelot.read_pdf("tables.pdf") tables.export("extracted.csv", f="csv", compress=True) # 使用tabula import tabula tabula.read_pdf("tables.pdf", pages="all") tabula.convert_into("table.pdf", "output.csv", output_format="csv", pages="all")</code>

9、截图

利用mss和PIL库进行屏幕截图操作:

<code># 使用mss from mss import mss with mss() as screenshot: screenshot.shot(output='scr.png') # 使用PIL import PIL.ImageGrab scr = PIL.ImageGrab.grab() scr.save("scr.png")</code>

10、拼写检查器

使用textblob和autocorrect库进行拼写检查:

<code># 使用textblob import textblob text = "mussage" print("original text: " + str(text)) checked = textblob.TextBlob(text) print("corrected text: " + str(checked.correct())) # 使用autocorrect import autocorrect spell = autocorrect.Speller(lang='en') # 示例 print(spell('cmputr')) print(spell('watr')) print(spell('survice'))</code>
© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容