图片转pdf
from PIL import Image
im = Image.open('cat.jpg')
im2.save('cat2.pdf', 'pdf')
直接把图片保存为pdf是图片的模式一定要为RGB模式,否则就要转换为RGB模式,转换方法为im.convert(mode)
pdf合并
def combine_pdf(files, name): output=PdfFileWriter() outputPages=0 if len(files) == 1: return files[0] for filename in files: # 读取源pdf文件 input=PdfFileReader(open(filename,"rb")) # 如果pdf文件已经加密,必须首先解密才能使用pyPdf if input.isEncrypted == True: input.decrypt("map") # 获得源pdf文件中页面总数 pageCount=input.getNumPages() outputPages+=pageCount # 分别将page添加到输出output中 for iPage in range(0,pageCount): output.addPage(input.getPage(iPage)) # 最后写pdf文件 i = 1 while os.path.exists(name): names = name.split('.') name = names[0] + str(i) + '.pdf' i += 1 outputStream=open(name,"wb") output.write(outputStream) outputStream.close() return name
if __name__ == "__main__": f1 ='/Users/cityking/Downloads/trade_contract/_20180523全球联交易宝平台使用协议-new2.pdf' f2 ='/Users/cityking/Downloads/trade_contract/6.pdf' print(combine_pdf([f1,f2], '/Users/cityking/Downloads/trade_contract/new_trade_contract.pdf'))