from spire.doc import *
from spire.doc.common import *
from pathlib import Path

# 删除原有的单文件读取逻辑，改为遍历文件夹并统计平均值
folder = Path("/home/ccran/lufa-contract/datasets/2-1.合同审核-近三年审核前的合同文件")  # 如需自定义目录，修改此处
lengths = []

for p in folder.iterdir():
    if not p.is_file():
        continue
    try:
        doc = Document()
        doc.LoadFromFile(str(p))
        text = doc.GetText()
        length = len(text)
        print(f"{p.name}: {length}")
        lengths.append(length)
    except Exception as e:
        print(f"跳过文件 {p.name}: {e}")

if lengths:
    avg = sum(lengths) / len(lengths)
    print(f"平均长度: {avg:.2f}（共 {len(lengths)} 个文件）")
else:
    print("没有可处理的文件。")