2025年4月15日 星期二 乙巳(蛇)年 正月十六 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

python将文字PDF转为图片PDF

时间:08-08来源:作者:点击数:26

效果看图1和图2

图1
图1
图2
图2
  • import pymupdf
  • import os
  • import shutil
  • from PIL import Image
  • input_pdf = "VBA常用技巧.pdf"
  • output_pdf = 'new_'+input_pdf
  • output_images = "output_images"
  • pdf_file = pymupdf.open(input_pdf)
  • if not os.path.exists(output_images): os.mkdir(output_images)
  • def convert_pdf_to_images():
  • for pagenum in range(pdf_file.page_count):
  • pdfpage = pdf_file[pagenum]
  • zoom_x, zoom_y = :2,2
  • mat = pymupdf.Matrix(zoom_x, zoom_y).prerotate(0)
  • pix = pdfpage.get_pixmap(matrix=mat, alpha=False)
  • if ((pagenum+1)>=0 and (pagenum+1)<=9):
  • pix.save(f"./"+output_images+"/000"+str(pagenum+1)+".png")
  • elif((pagenum+1)>=10 and (pagenum+1)<=99):
  • pix.save(f"./"+output_images+"/00"+str(pagenum+1)+".png")
  • elif((pagenum+1)>=100 and (pagenum+1)<=999):
  • pix.save(f"./"+output_images+"/0"+str(pagenum+1)+".png")
  • else:
  • pix.save(f"./"+output_images+str(pagenum+1)+".png")
  • def convert_images_to_pdf(input_dir, new_pdf):
  • image_files = [f for f in os.listdir(input_dir) if
  • f.endswith('.png') or f.endswith('.jpg')]
  • images = []
  • for image_path in image_files:
  • image_path = os.path.join(input_dir, image_path)
  • image = Image.open(image_path)
  • images.append(image.convert("RGB"))
  • images[0].save(new_pdf, save_all=True, append_images=images[1:])
  • def delete_folder(path):
  • try:
  • shutil.rmtree(path)
  • print(f"成功删除文件夹 {path}")
  • except OSError as e:
  • print(f"删除文件夹 {path} 失败: {e}")
  • # convert_pdf_to_images()
  • # convert_images_to_pdf(output_images,output_pdf)
  • # delete_folder(output_images)

 

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐