_tmp_inspect.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. """导出响应文件 PDF 并渲染抽查页,定位排版问题。"""
  3. import io
  4. import os
  5. import sys
  6. import time
  7. sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
  8. HERE = os.path.dirname(os.path.abspath(__file__))
  9. BID = r"C:\Users\skygu\OneDrive\Projects\AIDOP\项目\项目招投标"
  10. RESP = os.path.join(
  11. BID, "响应文件(完整版)-2026年河南产互联制造业数据智能运营平台研发项目.docx")
  12. PDF = os.path.join(HERE, "_tmp_inspect", "current.pdf")
  13. OUT = os.path.join(HERE, "_tmp_inspect")
  14. os.makedirs(OUT, exist_ok=True)
  15. PAGES = [int(x) for x in sys.argv[1].split(",")] if len(sys.argv) > 1 else \
  16. [1, 9, 16, 20, 21, 40, 45, 48, 64, 65, 80, 95, 96, 105, 150]
  17. def export():
  18. import win32com.client as win32
  19. word = None
  20. for i in range(4):
  21. try:
  22. word = win32.gencache.EnsureDispatch("Word.Application")
  23. word.Visible = False
  24. word.DisplayAlerts = 0
  25. break
  26. except Exception as e:
  27. print(f" 重试 {i + 1}/4:{e}")
  28. time.sleep(4)
  29. doc = word.Documents.Open(RESP, ReadOnly=True)
  30. try:
  31. doc.ExportAsFixedFormat(PDF, 17)
  32. finally:
  33. doc.Close(False)
  34. word.Quit()
  35. print(f"导出 {PDF} {os.path.getsize(PDF) / 1024 / 1024:.1f}MB")
  36. if not os.path.exists(PDF) or "--re" in sys.argv:
  37. export()
  38. import fitz
  39. d = fitz.open(PDF)
  40. print(f"PDF 共 {d.page_count} 页,渲染 {PAGES}")
  41. for p in PAGES:
  42. if p > d.page_count:
  43. continue
  44. pm = d[p - 1].get_pixmap(dpi=80)
  45. pm.save(os.path.join(OUT, f"p{p:03d}.png"))
  46. print(f" p{p} {pm.width}×{pm.height}")
  47. d.close()