55 lines
2.0 KiB
Python
55 lines
2.0 KiB
Python
import win32com.client as win32
|
|
import pythoncom
|
|
|
|
pythoncom.CoInitialize()
|
|
try:
|
|
word = win32.Dispatch("Word.Application")
|
|
word.Visible = False
|
|
|
|
doc = word.Documents.Open(r"c:\PPRO\PCM_Report\configs\600泵\template.docx")
|
|
|
|
print("=== 模板文档分析 ===")
|
|
print(f"表格数量: {doc.Tables.Count}")
|
|
|
|
if doc.Tables.Count > 0:
|
|
table = doc.Tables(1)
|
|
print(f"\n第一个表格:")
|
|
print(f" 行数: {table.Rows.Count}")
|
|
print(f" 列数: {table.Columns.Count}")
|
|
|
|
print("\n查找 {scriptTable1} token:")
|
|
found = False
|
|
for row_idx in range(1, min(5, table.Rows.Count + 1)):
|
|
row = table.Rows(row_idx)
|
|
for col_idx in range(1, min(10, row.Cells.Count + 1)):
|
|
try:
|
|
cell = row.Cells(col_idx)
|
|
text = cell.Range.Text
|
|
clean = text.replace('\r\x07', '').replace('\x07', '').strip()
|
|
if '{scriptTable1}' in clean or 'scriptTable' in clean:
|
|
print(f" 找到! 位置: 行{row_idx}, 列{col_idx}")
|
|
print(f" 原始文本: {repr(text[:50])}")
|
|
print(f" 清理后: {clean[:50]}")
|
|
found = True
|
|
except:
|
|
pass
|
|
|
|
if not found:
|
|
print(" 未找到 {scriptTable1}")
|
|
print("\n前3行前5列内容:")
|
|
for row_idx in range(1, min(4, table.Rows.Count + 1)):
|
|
print(f"\n 行{row_idx}:")
|
|
row = table.Rows(row_idx)
|
|
for col_idx in range(1, min(6, row.Cells.Count + 1)):
|
|
try:
|
|
cell = row.Cells(col_idx)
|
|
text = cell.Range.Text.replace('\r\x07', '').replace('\x07', '').strip()
|
|
print(f" 列{col_idx}: {text[:30]}")
|
|
except:
|
|
pass
|
|
|
|
doc.Close(False)
|
|
word.Quit()
|
|
finally:
|
|
pythoncom.CoUninitialize()
|