python统计当前文件夹pdf页数,并将结果以excel保存到当前文件夹 import os import openpyxl from PyPDF2 import PdfReader # 获取当前文件夹路径 current_directory = os.getcwd() # 创建一个Excel工作簿 workbook = openpyxl.Workbook() sheet = workbook.active sheet.title = 'PDF Pages' # 写入表头 sheet['A1'] = '文件名' sheet['B1'] = '页数' # 获取PDF文件夹中的所有PDF文件 pdf_files = [file for file in os.listdir(current_directory) if file.endswith('.pdf')] # 遍历每个PDF文件,获取页数并写入Excel文件 for idx, pdf_file in enumerate(pdf_files, start=2): file_path = os.path.join(current_directory, pdf_file) with open(file_path, 'rb') as f: pdf_reader = PdfReader(f) num_pages = len(pdf_reader.pages) sheet[f'A{idx}'] = pdf_file sheet[f'B{idx}'] = num_pages # 保存Excel文件 excel_file_path = os.path.join(current_directory, 'pdf_pages.xlsx') workbook.save(excel_file_path) print(f'已将PDF文件夹中的页数统计保存到 {excel_file_path}')