_tmp_check_sealed.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. """校验盖章版:与原件逐段比对文字,并渲染有章的页面便于目视确认。"""
  3. import io
  4. import os
  5. import sys
  6. import time
  7. from docx import Document
  8. sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
  9. HERE = os.path.dirname(os.path.abspath(__file__))
  10. BID = r"C:\Users\skygu\OneDrive\Projects\AIDOP\项目\项目招投标"
  11. NAME = "响应文件(完整版)-2026年河南产互联制造业数据智能运营平台研发项目.docx"
  12. SRC = os.path.join(BID, NAME)
  13. OUT = os.path.join(BID, NAME[:-5] + "(已签章).docx")
  14. PDF = os.path.join(HERE, "_tmp_inspect", "sealed.pdf")
  15. DIR = os.path.join(HERE, "_tmp_inspect")
  16. os.makedirs(DIR, exist_ok=True)
  17. PAGES = [1, 5, 6, 7, 8, 9, 12, 46, 61, 62, 65, 76, 79, 91]
  18. def compare():
  19. a, b = Document(SRC), Document(OUT)
  20. pa = [p.text for p in a.paragraphs]
  21. pb = [p.text for p in b.paragraphs]
  22. print(f"段落数:原件 {len(pa)} 盖章版 {len(pb)}")
  23. diff = 0
  24. for i, (x, y) in enumerate(zip(pa, pb)):
  25. if x != y:
  26. diff += 1
  27. if diff <= 8:
  28. print(f" × P{i}\n 原「{x[:56]}」\n 章「{y[:56]}」")
  29. print(f"段落文字差异 {diff} 处")
  30. ta = [c.text for t in a.tables for r in t.rows for c in r.cells]
  31. tb = [c.text for t in b.tables for r in t.rows for c in r.cells]
  32. print(f"表格数:原件 {len(a.tables)} 盖章版 {len(b.tables)};"
  33. f"单元格 {len(ta)} / {len(tb)},"
  34. f"文字差异 {sum(1 for x, y in zip(ta, tb) if x != y)} 处")
  35. ia = len(a.inline_shapes)
  36. ib = len(b.inline_shapes)
  37. print(f"嵌入式图片:原件 {ia} 盖章版 {ib}(应相等,印章走浮动图形)")
  38. def export():
  39. import win32com.client as win32
  40. word = None
  41. for i in range(4):
  42. try:
  43. word = win32.gencache.EnsureDispatch("Word.Application")
  44. word.Visible = False
  45. word.DisplayAlerts = 0
  46. break
  47. except Exception as e:
  48. print(f" 重试 {i + 1}/4:{e}")
  49. time.sleep(4)
  50. doc = word.Documents.Open(OUT, ReadOnly=True)
  51. try:
  52. print(f"浮动图形数:{doc.Shapes.Count}")
  53. doc.ExportAsFixedFormat(PDF, 17)
  54. finally:
  55. doc.Close(False)
  56. word.Quit()
  57. print(f"导出 {PDF} {os.path.getsize(PDF) / 1024 / 1024:.1f}MB")
  58. compare()
  59. if not os.path.exists(PDF) or "--re" in sys.argv:
  60. export()
  61. import fitz
  62. d = fitz.open(PDF)
  63. print(f"\nPDF 共 {d.page_count} 页,渲染 {PAGES}")
  64. for p in PAGES:
  65. if p <= d.page_count:
  66. d[p - 1].get_pixmap(dpi=100).save(os.path.join(DIR, f"s{p:03d}.png"))
  67. print("完成")
  68. d.close()