本文记录了图片下载后转换成PDF的过程,期间用到了EXCEL和PYTHON,所有来源都是从网络上学习后,稍加整理并测试通过。
[阶段一:有规则的图片下载地址]
之所以用Excel下载图片,主要是电脑上没有下载工具,于是找到以下代码,并稍加修改后,就可以完成批量下载了(我们不谈速度23333)
1 Option Explicit2 Private Declare Function MakeSureDirectoryPathExists _3 Lib "imagehlp.dll"(ByVal Pfad As String) As Long4 Private Declare Function DeleteUrlCacheEntry Lib "wininet"Alias _5 "DeleteUrlCacheEntryA"(ByVal lpszUrlName As String) As Long6 Private Declare Function URLDownloadToFile Lib "urlmon"_7 Alias "URLDownloadToFileA"( _8 ByVal pCaller As Long, _9 ByVal szURL As String, _10 ByVal szFileName As String, _11 ByVal dwReserved As Long, _12 ByVal lpfnCB As Long) As Long13
14 Public Sub GetFiles()15 Const strBackup As String = "D:\poto\" ‘保存路径16 Dim arr17 Dim TargetFile As String18 Dim lngTMP, i As Long19 Dim cnt20
21 arr = Range("a1").CurrentRegion22 MakeSureDirectoryPathExists strBackup23 cnt = UBound(arr)24 For i = 2 To cnt25 TargetFile = strBackup & arr(i, 2) & ".jpg"‘A列26 Call DeleteUrlCacheEntry(arr(i, 1)) ‘B列27 lngTMP = URLDownloadToFile(0, arr(i, 1), TargetFile, 0, 0)28 DoEvents29 Application.StatusBar = "结束前请勿乱动:" & i & "/" & cnt
30 Next31
32 Application.StatusBar = cnt & "/" & cnt & " 完成。"
33 End Sub
[阶段二:将图片转PDF]
本来以为这个很容易,随便下载一个软件转换一下就行了,没想到尝试了好多软件,都需要付费,而且大多数是大于5张图片就100%需要收费了。正好最近在看python,基础语法什么的已经了解了,于是网络上搜刮了一下:
源码:
1 from PIL importImage2 importos3 importre4
5
6 def tryint(s): #将元素中的数字转换为int后再排序
7 try:8 returnint(s)9 exceptValueError:10 returns11
12 def str2int(v_str): #将元素中的字符串和数字分割开
13 return [tryint(sub_str) for sub_str in re.split(‘([0-9]+)‘, v_str)]14
15 def sort_humanly(v_list): #以分割后的list为单位进行排序
16 return sorted(v_list, key=str2int)17 defrea(path, pdf_name):18 file_list =os.listdir(path)19 pic_name =[]20 im_list =[]21 for x infile_list:22 if "jpg" in x or ‘png‘ in x or ‘jpeg‘ inx:23 pic_name.append(x)24
25 pic_name=sort_humanly(pic_name)26 print(‘---‘)27 print(pic_name)28 new_pic =[]29
30 for x inpic_name:31 if "jpg" inx:32 new_pic.append(x)33
34 for x inpic_name:35 if "png" inx:36 new_pic.append(x)37
38 print("hec", new_pic)39
40 im1 =Image.open(os.path.join(path, new_pic[0]))41 new_pic.pop(0)42 for i innew_pic:43 img =Image.open(os.path.join(path, i))44 #im_list.append(Image.open(i))
45 if img.mode == "RGBA":46 img = img.convert(‘RGB‘)47 im_list.append(img)48 else:49 im_list.append(img)50 im1.save(pdf_name, "PDF", resolution=100.0, save_all=True, append_images=im_list)51 print("输出文件名称:", pdf_name)52
53
54 if __name__ == ‘__main__‘:55
56 mypath = input("输入文件路径>")57 pdf_name = mypath + r‘\result.pdf‘
58 if ".pdf" inpdf_name:59 rea(mypath, pdf_name=pdf_name)60 else:61 rea(mypath, pdf_name="{}.pdf".format(pdf_name))
这里面涉及一个排序问题,就是1.JPG、2.JPG、10.JPG等,用了默认的.sort之后,排序是1.JPG、10.JPG、2.JPG。有点问题,于是又重新找了一个排序放上去就正常了,特别网上分享经验的各位Up主。下面就是这个神奇的排序:
1 def tryint(s): #将元素中的数字转换为int后再排序
2 try:3 returnint(s)4 exceptValueError:5 returns6
7 def str2int(v_str): #将元素中的字符串和数字分割开
8 return [tryint(sub_str) for sub_str in re.split(‘([0-9]+)‘, v_str)]9
10 def sort_humanly(v_list): #以分割后的list为单位进行排序
11 return sorted(v_list, key=str2int)
资源来源网上,为个人整理,欢迎大家提意见~!!!