0%

一些没用的python脚本

pdf2docx

exe下载

文件 (lanzouv.com)
密码:0000

1
pip install pdf2docx

代码

1
2
3
4
5
6
7
8
9
10
11
12
from pdf2docx import Converter

def pdf_to_word(pdf_file, word_file):
cv = Converter(pdf_file)
cv.convert(word_file, start=0, end=None)
cv.close()
print(f'转换完成:{word_file}')

pdf_file = 'input.pdf' # 替换为 PDF 文件路径
word_file = 'output.docx' # 输出的 Word 文件路径

pdf_to_word(pdf_file, word_file)

按日期整理图片视频

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import shutil
from datetime import datetime
import time
def get_creation_date(file_path):
return os.path.getmtime(file_path)

def get_file_size(file_path):
return os.path.getsize(file_path)

def classify_files(source_folder, target_folder):
file_count = 0
total_size = 0
for filename in os.listdir(source_folder):
file_path = os.path.join(source_folder, filename)

if os.path.isfile(file_path):
creation_time = get_creation_date(file_path)
creation_date = datetime.fromtimestamp(creation_time)

year = creation_date.strftime('%Y')
month = creation_date.strftime('%m')
day = creation_date.strftime('%d')

target_path = os.path.join(target_folder, year, month, day)
os.makedirs(target_path, exist_ok=True)

file_size = get_file_size(file_path)
total_size += file_size

shutil.move(file_path, os.path.join(target_path, filename))
print(f'文件 {filename} 已移动到 {target_path}')
file_count += 1

return file_count, total_size

source_folder = 'D:/PHOTO' # 替换为你的源文件夹路径
target_folder = 'D:/Xiaomi13Photo' # 替换为你的目标文件夹路径

start_time = time.time()
processed_files, total_size = classify_files(source_folder, target_folder)
end_time = time.time()
elapsed_time = end_time - start_time
print(f'程序运行耗时: {elapsed_time:.2f} 秒')
if total_size > (1024 * 1024 * 1024):
print(f'共处理了 {processed_files} 个文件,文件总大小: {total_size / (1024 * 1024 * 1024):.2f} GB')
else:
print(f'共处理了 {processed_files} 个文件,文件总大小: {total_size / (1024 * 1024):.2f} MB')

共5789张照片

image-20240921083656995

image-20240921084812939 image-20240921084859259

迁移C盘自动链接脚本

如何使用请看:清理C盘之数据迁移 | ZYG’s Notes (creeeeeeeeeeper.github.io)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import os
import shutil

original_catalogue_path = input("需迁移目录路径: ")
new_catalogue_path = 'D:\\AimportantDataLink' + "\\" + original_catalogue_path.split("\\")[-1]

def move_catalogue(original_catalogue_path, new_catalogue_path):
if not os.path.exists(original_catalogue_path):
print("原目录不存在!")
return
if not os.path.exists(new_catalogue_path):
os.makedirs(new_catalogue_path)
for item in os.listdir(original_catalogue_path):
original_item_path = os.path.join(original_catalogue_path, item)
new_item_path = os.path.join(new_catalogue_path, item)
shutil.move(original_item_path, new_item_path)
print(f"已移动: {original_item_path} -> {new_item_path}")
link_name = new_catalogue_path
command = f'mklink "{original_catalogue_path}" "{link_name}"'
shutil.rmtree(original_catalogue_path)
os.system(command)
print(f"已创建链接: {link_name} -> {new_catalogue_path}")
move_catalogue(original_catalogue_path, new_catalogue_path)