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
| ''' @Author: Xhy @Fix: kelffon wang @Description: SWF2PDF @LastEditTime: 2020-04-16 23:51:36 ''' from PIL import Image import os import sys import requests
urlStart = "http://ebooks.crup.com.cn/t/xxxxx_output/web/xxxxx_opf_files/xxxxxxxxxxxxxxxxxxxx_" startPage = 1 endPage = 361 minDepartPage = 120 departPage = 60
def getPic(i): url = urlStart + str(i) + ".swf" res = requests.get(url) if(res.status_code == 200): with open(str(i)+'.swf', 'wb') as file: file.write(res.content) print(i, "Swf Saved") else: print(res.status_code, "Failed") sys.exit()
def mergePng2Pdf(s, e): imgs = [] first_img = Image.open(str(s)+".png").convert('RGB') for i in range(s+1, e): imgs.append(Image.open(str(i)+".png").convert('RGB')) first_img.save('book_'+str(s)+'.pdf', 'PDF', resolution=100.0, save_all=True, append_images=imgs) print("PDF Saved.",'location: book_'+str(s)+'.pdf')
def do_download(): for i in range(startPage, endPage+1): if(not os.access(str(i)+".png", os.F_OK) and not os.access(str(i)+".swf", os.F_OK)): getPic(i)
def do_convert(): for i in range(startPage, endPage+1): if(os.access(str(i)+".swf", os.F_OK) and not os.access(str(i)+".png", os.F_OK)): execstr = "swfrender.exe " + \ str(i)+".swf -X 3200 -Y 4800 -o "+str(i)+".png" val = os.system(execstr) if(val == 0): print(i, "Png Saved.")
def do_merge(): if (endPage-startPage) < minDepartPage: mergePng2Pdf(startPage, endPage) else: depart = int((endPage-startPage)/departPage)+1 for i in range(depart): s = startPage+i*departPage t = s+departPage pdfname = 'book_'+str(s)+'.pdf' if(not os.access(pdfname, os.F_OK)): print("PDF", i+1, "building...") if(s > endPage): s = endPage if(t > endPage): t = endPage mergePng2Pdf(s, t) else: print("PDF", i+1, "existed!")
do_download() do_convert() do_merge()
|