收集的python实现的一些小工具脚本

1 查询计算的CPU、Mem和Disk等配置信息

# 获取计算机规格配置信息
import wmi


def system_spec_info():
    computer = wmi.WMI()
    os_info = computer.Win32_OperatingSystem()[0]
    processor = computer.Win32_Processor()[0]
    gpu = computer.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_info()

2 文件解压缩

# 解压文件
from zipfile import ZipFile as zf

unzip = zf("demo.zip", "r")
unzip.extractall("demo")

3 excel文件同类型数据表合并

import pandas as pd

# 文件名
filenames = ["test1.xlsx", "test2.xlsx", "test3.xlsx", "test4.xlsx", "test5.xlsx"]
# 表格数量
T_sheets = len(filenames)

df = []
for i in range(0, T_sheets):
    sheet_data = pd.read_excel(filenames[i], sheet_name=1, header=None)
    df.append(sheet_data)

# 合并表格
output = "test_merge.xlsx"
df = pd.concat(df)
df.to_excel(output)

4 获取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)

5 解析PDF中的表格数据

有的时候,我们需要从PDF中提取表格数据。
第一时间你可能会先想到手工整理,但是当工作量特别大,手工可能就比较费劲。
然后你可能会想到一些软件和网络工具来提取 PDF 表格。
下面这个简单的脚本将帮助你在一秒钟内完成相同的操作。

# 方法一
import camelot

tables = camelot.read_pdf("tables.pdf")
print(tables)
tables.export("extracted.csv", f="csv", compress=True)
# 方法二, 需要安装JDK
import tabula

tabula.read_pdf("tables.pdf", pages="all")
tabula.convert_into("table.pdf", "output.csv", output_format="csv", pages="all")

6 截图

脚本将简单地截取屏幕截图,而无需使用任何屏幕截图软件。
在下面的代码中,给大家展示了两种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")

7 拼写检查器

这个Python脚本可以进行拼写检查,当然只对英文有效。

# 拼写检查方法一
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')

# 以英语为例
print(spell('cmputr'))
print(spell('watr'))
print(spell('survice'))

8 将图像转换为素描图

和之前的图片格式转换有点类似,就是对图像进行处理。其实使用Python的OpenCV,就能够快速实现很多你想要的效果。

# 图像转换
import cv2

# 读取图片
img = cv2.imread("test.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()

9 加解密PDF文档

如果你有100个或更多的PDF文件需要加密,手动进行加密肯定是不可行的,极其浪费时间。
使用Python的pikepdf模块,即可对文件进行加密,写一个循环就能进行批量加密文档。

# PDF加密
import pikepdf

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

pdf = pikepdf.open("encrypt.pdf",  password='你要设置的密码')
pdf.save("decrypt.pdf")
pdf.close()

10 加解密execl文档

# excel加解密
import win32com.client

def excel_encryption(file_path, passwd):
	excel_app = win32com.client.Dispatch('Excel.Application')
	excel_app.Visible = 0
	excel_app.DisplayAlerts = 0
	wb = excel_app.Workbooks.Open(file_path, False, False, None, Password='')
	wb.SaveAs(file_path, None, passwd, '')
	wb.Close()
	excel_app.Quit()

11 加解密word文档

# excel加解密
import win32com.client

def excel_encryption(file_path, passwd):
def word_encryption(file_path, path_temp, passwd):
	word_app = win32com.client.Dispatch('Word.Application')
	word_app.Visible = 0
	word_app.DisplayAlerts = 0
	doc = word_app.Documents.Open(path_temp, False, False, False, '1')
	doc.SaveAs2(path_temp, None, False, passwd)
	doc.Close()
	word_app.Quit()
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值