# -*- coding: utf-8 -*- """将 ACP.pdf 逐页渲染为 PNG,并把 Excel 接线图导出为文本。""" import fitz import openpyxl import os pdf_path = r"C:\Users\kk\Desktop\ACP\ACP电路图\ACP.pdf" xlsx_path = r"C:\Users\kk\Desktop\ACP\ACP电路图\ACP硬件接线图.xlsx" out_dir = r"C:\Users\kk\Desktop\ACP\ACP电路图\_pages" os.makedirs(out_dir, exist_ok=True) # ---- 渲染 PDF ---- doc = fitz.open(pdf_path) mat = fitz.Matrix(2.0, 2.0) # 2倍缩放,保证清晰度 for i, page in enumerate(doc): pix = page.get_pixmap(matrix=mat) png_path = os.path.join(out_dir, f"page_{i+1:02d}.png") pix.save(png_path) print(f"saved {png_path}") doc.close() # ---- 导出 Excel ---- wb = openpyxl.load_workbook(xlsx_path, data_only=True) txt_path = os.path.join(out_dir, "接线图.txt") with open(txt_path, "w", encoding="utf-8") as f: for sheet_name in wb.sheetnames: ws = wb[sheet_name] f.write(f"===== Sheet: {sheet_name} ({ws.max_row}行x{ws.max_column}列) =====\n") for row in ws.iter_rows(values_only=True): cells = ["" if c is None else str(c).strip() for c in row] # 去掉行尾空单元格 while cells and cells[-1] == "": cells.pop() if not cells: continue f.write(" | ".join(cells) + "\n") f.write("\n") print(f"saved {txt_path}")