python 提取当前目录pdf文件第一页为图片格式,并保存到当前文件夹 import os import subprocess from PyPDF2 import PdfReader def extract_first_page_as_image(pdf_filename): # Check if the PDF file exists if not os.path.exists(pdf_filename): print(f"File {pdf_filename} does not exist.") return # Get the current directory current_directory = os.getcwd() # Define the output image filename output_image_filename = f"{pdf_filename[:-4]}.jpg" # Run the command to convert PDF to image using pdftoppm (part of Poppler) subprocess.run(["pdftoppm", "-jpeg", pdf_filename, output_image_filename]) def main(): # Get the current directory current_directory = os.getcwd() # List all files in the current directory files = os.listdir(current_directory) # Iterate through each file for file in files: # Check if the file is a PDF file if file.endswith(".pdf"): # Extract the first page as an image extract_first_page_as_image(file) if __name__ == "__main__": main()