import fitz import os pdf_file_path="sample/GB.pdf" pdf=fitz.open(pdf_file_path) save_dir="cache/GB" os.makedirs(save_dir,exist_ok=True) for page_num in range(len(pdf)): page=pdf[page_num] pix=page.get_pixmap() image_filename=os.path.join(save_dir,f"{os.path.splitext(os.path.basename(pdf_file_path))[0]}_page_{page_num+1}.png") pix.save(image_filename) pdf.close()