Tomyi 发表于 2025-4-1 15:23

批量将excel文件转PDF文件(纯Python代码版)

#批量excel文件转PDF文件
import time
import win32com.client
from pathlib import Path
import logging

# 配置日志记录
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def convert_excel_to_pdf(input_path: Path):
    excel = None
    workbook = None
    try:
      # 启动独立的Excel进程
      excel = win32com.client.DispatchEx("Excel.Application")
      excel.Visible = False
      excel.DisplayAlerts = False

      # 检查文件是否存在
      if not input_path.exists():
            raise FileNotFoundError(f"文件不存在: {input_path}")

      # 打开工作簿
      workbook = excel.Workbooks.Open(str(input_path))

      # 生成输出路径(同目录)
      output_path = input_path.with_suffix('.pdf')

      # 执行转换
      workbook.ExportAsFixedFormat(0, str(output_path))
      logging.info(f"[✓] 成功转换: {input_path.name}")
      return input_path
    except FileNotFoundError as e:
      logging.error(f"[×] 转换失败: {input_path.name}")
      logging.error(f"    错误详情: {str(e)}")
    except Exception as e:
      logging.error(f"[×] 转换失败: {input_path.name}")
      logging.error(f"    错误详情: {str(e)}")
    finally:
      # 资源释放(增加关闭重试机制)
      if workbook is not None:
            for _ in range(3):
                try:
                  workbook.Close(False)
                  break
                except:
                  time.sleep(0.5)
      if excel is not None:
            for _ in range(3):
                try:
                  excel.Quit()
                  break
                except:
                  time.sleep(0.5)
            import os
            os.system('taskkill /f /im excel.exe')
    return None

def batch_convert(root_folder: Path):
    file_count = 0
    start_time = time.time()
    converted_files = []

    # 遍历所有Excel文件(包括.xlsx和.xls)进行转换
    for file in root_folder.rglob('*.xlsx'):
      if not file.name.startswith('~$'):
            result = convert_excel_to_pdf(file)
            if result:
                converted_files.append(result)
            file_count += 1

    for file in root_folder.rglob('*.xls'):
      if not file.name.startswith('~$'):
            result = convert_excel_to_pdf(file)
            if result:
                converted_files.append(result)
            file_count += 1

    # 全部转换完成后,批量删除原文件
    for file in converted_files:
      try:
            file.unlink()
            logging.info(f"    ░ 已删除原文件: {file.name}")
      except PermissionError:
            logging.error(f"    无法删除文件: {file.name},文件可能仍被占用。")

    logging.info(f"\n处理完成!共转换{file_count}个文件,耗时{time.time() - start_time:.1f}秒")

if __name__ == '__main__':
    target_folder = Path(r'C:\Users\托咪\Desktop\data')   # 改为你自己要处理的路径

    if not target_folder.exists():
      logging.error(f"错误:目标文件夹不存在 - {target_folder}")
    else:
      logging.info("=== Excel转PDF自动清理工具 ===")
      logging.info("操作说明:")
      logging.info("1. PDF将保存至原文件所在目录")
      logging.info("2. 转换成功的Excel文件会被自动删除\n")
      batch_convert(target_folder)


遍历路径下的所有文件夹与子文件夹,转换后删除原有excel表格,如要保留原文件,请提前自行备份!

nicle 发表于 2025-6-21 19:34

我运行时出现ModuleNotFoundError: No module named 'win32com' 错误,为什么?

注:pywin32已经安装成功了。
页: [1]
查看完整版本: 批量将excel文件转PDF文件(纯Python代码版)