这里提供一个打包好的剪切包window环境打开即可运行123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172importosimportfitzimportglobimportrequestsos.environ[NLS_LANG]SIMPLIFIED CHINESE_CHINA.UTF8#剪切pdf为图片defCutPdf(pdfPath,savePath,pdfSavePath):try:docfitz.open(pdfPath)#pdf路径forpginrange(doc.pageCount):pagedoc[pg]rotateint(0)# 每个尺寸的缩放系数为2这将为我们生成分辨率提高四倍的图像。zoom_x2.0zoom_y2.0transfitz.Matrix(zoom_x, zoom_y).preRotate(rotate)pmpage.getPixmap(matrixtrans, alphaFalse)imagePathsavePath%s.png%str(pg1)pm.writePNG(imagePath)#最终存储路径newpdfPathpdfSavePath%s.pdf%str(pg1)print(剪切一张图片str(pg1))frompic2pdf(imagePath,pdfSavePath,newpdfPath)print(转换成功一张pdfstr(pg1))exceptException as e:print(CutPdf出现异常:str(e))#图片转pdfdeffrompic2pdf(img_path,pdfSavePath, pdf_path):# 使用glob读图forimginsorted(glob.glob(img_path)):ifnotos.path.exists(pdfSavePath):os.makedirs(pdfSavePath)# 打开空文档docfitz.open()# 打开指定图片imgdocfitz.open(img)# 使用图片创建单页的PDFpdfbytesimgdoc.convertToPDF()imgpdffitz.open(pdf, pdfbytes)# 将当前页写入文档doc.insertPDF(imgpdf)# 保存为指定名称的PDF文件doc.save(pdf_path)# 关闭doc.close()defmain():pdfUrlcardsfileListos.listdir(pdfUrl)index1foriteminfileList:print(开始裁剪item)urlpdfUrl/itemfoldStrimages/item.split(.)[0]url2foldStr/item.split(.)[0]foldStr3pdf/item.split(.)[0]url3foldStr3/ifnotos.path.exists(foldStr):os.makedirs(foldStr)CutPdf(url,url2,url3)print(完成裁剪)index1if__name____main__:main()知识补充除了上文的方法小编还为大家整理了其他Python裁剪pdf文件为图片的方法希望对大家有所帮助方法一剪裁PDF页面在剪裁PDF页面之前我们需要获取页面的尺寸并计算剪裁区域的位置和大小。下面是剪裁PDF页面的代码123456789101112131415# 获取页面尺寸page_widthpage.mediaBox.getWidth()page_heightpage.mediaBox.getHeight()# 定义剪裁区域的位置和大小x100# 起始点的x坐标y100# 起始点的y坐标width200# 剪裁区域的宽度height300# 剪裁区域的高度# 剪裁PDF页面page.trimBox.lowerLeft(x, y)page.trimBox.upperRight(xwidth, yheight)page.cropBox.lowerLeft(x, y)page.cropBox.upperRight(xwidth, yheight)代码解析使用mediaBox属性获取页面的宽度和高度。定义剪裁区域的位置和大小可以根据实际需求进行调整。使用trimBox属性设置剪裁后的页面的边界框。使用cropBox属性设置剪裁后的页面的裁剪框。保存剪裁后的图片最后我们需要将剪裁后的页面保存为图片。这可以通过使用PyPDF2库和Pillow库来实现。下面是保存剪裁后的图片的代码1234567fromPILimportImage# 将剪裁后的页面保存为图片withopen(output.jpg,wb) as image_file:page_datapage.extract_xobject().get(/Im0).getData()imageImage.open(io.BytesIO(page_data))image.save(image_file,JPEG)首先导入Pillow库中的Image模块用于处理图像。使用extract_xobject()方法从剪裁后的页面中提取图像数据。使用get(/Im0)方法获取图像数据。使用getData()方法获取图像的二进制数据。使用Image.open()方法打开图像数据。使用save()方法保存图像数据为JPEG格式的图片。output.jpg为保存的图片路径可以根据需要修改。方法二python分割pdf12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849fromPyPDF2importPdfFileReader, PdfFileWriter# PDF文件分割defsplit_pdf(read_file, out_detail):try:fp_read_fileopen(read_file,rb)pdf_inputPdfFileReader(fp_read_file)# 将要分割的PDF内容格式话page_countpdf_input.getNumPages()# 获取PDF页数print(page_count)# 打印页数withopen(out_detail,r,True,utf-8)as fp:# print(fp)txtfp.readlines()# print(txt)fordetailintxt:# 打开分割标准文件# print(type(detail))pages, write_filedetail.split()# 空格分组# write_file, write_ext os.path.splitext(write_file) # 用于返回文件名和扩展名元组pdf_filerC:\Users\GZTSALFIEL\Desktop\excel问题文件\pdf\\f{write_file}.pdf# liststrlist(map(int, pages.split(-)))# print(type(liststr))start_page, end_pagelist(map(int, pages.split(-)))# 将字符串数组转换成整形数组start_page-1try:print(f开始分割{start_page}页-{end_page}页保存为{pdf_file}......)pdf_outputPdfFileWriter()# 实例一个 PDF文件编写器foriinrange(start_page, end_page):pdf_output.addPage(pdf_input.getPage(i))withopen(pdf_file,wb) as sub_fp:pdf_output.write(sub_fp)print(f完成分割{start_page}页-{end_page}页保存为{pdf_file}!)exceptIndexError:print(f分割页数超过了PDF的页数)# fp.close()exceptException as e:print(e)finally:fp_read_file.close()# def main():# fire.Fire(split_pdf)## if __name__ __main__:# main()split_pdf(r..\pdfdocement\myfile.pdf,consult.txt)到此这篇关于Python实现快速将pdf文件剪切成多个图片的文章就介绍到这了