CS

Just some hand-writing notes

Still updating…

The reason I chose hand-writing instead of typing is that, I’ve just finished my High Shcool learning and I’ve been using hand-writing notes for years. It’s more convenient for myself, but maybe not so friendly to others. Sorry about this!

Maybe later in CSC courses I will try to use typing to note, after my $\LaTeX$ learning.

MAT1001 Calculus I

This is the first note I’ve done in CUHKSZ, so it may be a bit messy. The notes later will be more formal and tidy (I guess).

MAT1002 Calculus II

Unfinished

MAT2041 Linear Algebra and Applications

Unfinished

STA2001 Probability and Statistics I

Unfinished

Transform ZIP(jpgs) to PDF

Python Code

Generated by Deepseek

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import zipfile
from PIL import Image
from fpdf import FPDF
import glob

def zip_to_pdf(zip_path, output_folder=None):
"""
将ZIP文件中的JPG图片合并为PDF

参数:
zip_path (str): ZIP文件路径
output_folder (str, optional): 输出PDF的目录,默认为ZIP所在目录
"""
# 检查ZIP文件是否存在
if not os.path.exists(zip_path):
raise FileNotFoundError(f"ZIP文件不存在: {zip_path}")

# 设置输出目录
if output_folder is None:
output_folder = os.path.dirname(zip_path)

# 获取ZIP文件名(不带扩展名)
zip_name = os.path.splitext(os.path.basename(zip_path))[0]
pdf_path = os.path.join(output_folder, f"{zip_name}.pdf")

# 创建临时目录存放解压文件
temp_dir = os.path.join(output_folder, f"temp_{zip_name}")
os.makedirs(temp_dir, exist_ok=True)

try:
# 解压ZIP文件
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(temp_dir)

# 获取所有JPG文件
jpg_files = glob.glob(os.path.join(temp_dir, "*.jpg")) + glob.glob(os.path.join(temp_dir, "*.jpeg"))

if not jpg_files:
raise ValueError("ZIP文件中没有找到JPG图片")

# 按文件名排序
jpg_files.sort()

# 创建PDF
pdf = FPDF(unit="pt") # 使用点(point)作为单位

for jpg_file in jpg_files:
# 打开图片获取尺寸
with Image.open(jpg_file) as img:
width, height = img.size

# 添加新页面并设置尺寸
pdf.add_page(format=[width, height])
pdf.image(jpg_file, x=0, y=0, w=width, h=height)

# 保存PDF
pdf.output(pdf_path)
print(f"成功创建PDF: {pdf_path}")

finally:
# 清理临时文件
import time
time.sleep(1) # 等待1秒确保文件被释放

for root, dirs, files in os.walk(temp_dir, topdown=False):
for name in files:
file_path = os.path.join(root, name)
try:
os.remove(file_path)
except PermissionError:
print(f"无法删除文件 {file_path},可能仍被占用")
for name in dirs:
os.rmdir(os.path.join(root, name))
try:
os.rmdir(temp_dir)
except OSError:
print(f"无法删除临时目录 {temp_dir},可能仍有文件被占用")

if __name__ == "__main__":
# 示例用法
zip_file = input("请输入ZIP文件路径: ").strip('"\' ') # 处理可能的引号
zip_to_pdf(zip_file)

本站由 Sky Zhou 使用 Stellar 1.29.1 主题创建。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。
© Copyright skyzhou.top
All Rights Reserved