写个脚本把电脑上的下载的漫画合成一个Pdf文件方便在手机上看。
直接上脚本,依然不喜欢写注释。哈哈!
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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2023-10-27 21:06:41
# @Author : Xzap (xzap@163.com)
# @Link : http://xzap.gitee.io
# @Version : $Id$
from reportlab.pdfgen import canvas
from PIL import Image
from pathlib import Path
import uuid
class mypdf(object):
def __init__(self, path, pdfname):
self.path = path
self.pdfname = pdfname
self.c = canvas.Canvas(self.pdfname)
self.c.setPageCompression(0)
self.c.setAuthor("xzap")
title = str(Path(self.path).stem)
print (title)
self.c.setTitle(title)
self.c.setSubject(title)
self.iter_dir()
self.save()
def one_page(self, image_org):
u = str(uuid.uuid4())
image = Image.open(image_org)
w, h = image.size
self.c.setPageSize(image.size)
self.c.bookmarkPage(u)
self.c.addOutlineEntry(image_org.name,u, level=1)
self.c.drawImage(image_org, 0, 0, w, h)
image.close()
self.c.showPage()
def images_to_pdf(self, path):
p2 = Path(path)
print (p2.name)
u = str(uuid.uuid4())
self.c.bookmarkPage(u)
self.c.addOutlineEntry(str(p2.name),u, level=0)
images = [f for f in p2.iterdir() if str(f).lower().endswith('.jpg') or str(f).lower().endswith('.png')]
images.sort()
for i, image_org in enumerate(images):
self.one_page(image_org)
def iter_dir(self):
p = Path(self.path)
for i in p.iterdir():
if i.is_dir():
self.images_to_pdf(i)
def save(self):
self.c.save()
def main():
p = Path.cwd()
pdf = p / f"{p.name}.pdf"
mypdf(str(p), str(pdf))
if __name__ == '__main__':
main()
|