python脚本
1,图片类型转换
-
#!/usr/bin/env python # 图片格式转换, jpg转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加密和解密
-
#!/usr/bin/env python # 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()
3,获取电脑的配置信息
-
#!/usr/bin/env python # 获取计算机信息 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}') print("\n计算机信息如上 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑") System_spec()
4,解压文件
-
#!/usr/bin/env python # 解压文件 from zipfile import ZipFile unzip = ZipFile("file.zip", "r") unzip.extractall("output Folder")
5、Excel工作表合并
-
#!/usr/bin/env python 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)
6、将图像转换为素描图
-
#!/usr/bin/env python # 图像转换 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温度
-
#!/usr/bin/env python # 获取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表格
-
#!/usr/bin/env python # 方法一 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")
9、截图
-
#!/usr/bin/env 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、拼写检查器
-
# 拼写检查 # 方法一 import textblob text = "meassage" print("original text: " + str(text)) checked = textblob.TextBlob(text) print("corrected text: " + str(checked.correct())) # 方法二 import autocorrect spell = autocorrect.Speller(lang='en') # 以英语为例 print(spell('cmputr')) print(spell('watr')) print(spell('survice'))
shell脚本
1,shell脚本构成
- Shebang(#!)行: 这是脚本文件的第一行,用来指定解释器路径。
- 例如,#!/bin/bash 表示要使用Bash解释器执行该脚本。
- 注释: 注释用于给脚本添加说明性文字,以便他人阅读和理解脚本的用途和实现方法。
- 变量定义: 可以在脚本中定义各种类型的变量。
- 如字符串变量、整数变量、数组等。
- 函数定义: 通过函数定义,可以将一系列命令封装起来,实现代码的模块化和复用。
- 流程控制结构: Shell脚本支持各种流程控制结构,如 if-then-else 语句、for 循环、while 循环等,用于实现条件判断和循环执行。
- 命令执行: Shell脚本中最主要的部分是包含需要执行的命令,可以是系统命令、自定义命令或者其他脚本。
-
#!/bin/bash # 这是一个简单的Shell脚本示例 # 定义变量 NAME="World" # 函数定义 say_hello() { echo "Hello, $NAME!" } # 调用函数 say_hello