diff --git a/server/file_association.py b/server/file_association.py index c71f6ed..452e9b4 100644 --- a/server/file_association.py +++ b/server/file_association.py @@ -136,18 +136,45 @@ def read_docx_content(doc_path: Path) -> List[str]: has_tab_in_first_rows = any('\t' in line for line in lines[:6]) if not has_tab_in_first_rows and len(lines) >= 6: - # 可能是每行一个字段,尝试合并为 6 段 + # 判断是否是 6 行一组的模式:第二行是网址且第三行是日期/时间 + def is_url_line(line: str) -> bool: + """判断一行是否是网址""" + return bool(re.match(r'^https?://', line.strip())) + + def is_datetime_line(line: str) -> bool: + """判断一行是否是日期或时间""" + datetime_pattern = r'^\d{4}[-/]\d{1,2}[-/]\d{1,2}\s+\d{1,2}:\d{2}' + date_pattern = r'^\d{4}[-/]\d{1,2}[-/]\d{1,2}' + return bool(re.match(datetime_pattern, line.strip())) or \ + bool(re.match(date_pattern, line.strip())) + + # 检查当前起始位置是否是 6 行一组模式 + def is_six_row_group(lines: list, start_idx: int) -> bool: + """检查从 start_idx 开始是否是 6 行一组""" + if start_idx + 1 < len(lines): + return is_url_line(lines[start_idx + 1]) and \ + is_datetime_line(lines[start_idx + 2]) + return False + + # 确定起始偏移量 + offset = 0 + if is_six_row_group(lines, 0): + offset = 0 + elif len(lines) >= 3: + # 丢弃第一行,检查第二行是否是网址且第三行是否是日期/时间 + offset = 1 + + # 按确定的行数组模式处理所有行 result = [] - i = 0 + i = offset + while i + 5 <= len(lines): + combined = '\t'.join(lines[i:i+6]) + result.append(combined) + i += 6 + # 处理剩余行 while i < len(lines): - if i + 5 <= len(lines): - # 尝试合并接下来的 6 行作为一段 - combined = '\t'.join(lines[i:i+6]) - result.append(combined) - i += 6 - else: - result.append(lines[i]) - i += 1 + result.append(lines[i]) + i += 1 return result else: # 每行已经有\t分割,直接返回 @@ -510,7 +537,21 @@ def main(): twole_folder = base_dir / "21世纪教育网" zxxk_folder = base_dir / "学科网" - if not twole_folder.exists() or not zxxk_folder.exists(): + if not zxxk_folder.exists(): + print("❌ 文件夹不存在,请先解压文件") + # 将失败的原始压缩包拷贝到本程序所在目录的 ee 子目录 + script_dir = Path(__file__).parent + ee_dir = script_dir / "ee" + ee_dir.mkdir(exist_ok=True) + dest_file = ee_dir / zip_file.name + import shutil + shutil.copy2(zip_file, dest_file) + print(f" 已拷贝失败文件到:{dest_file}") + fail_count += 1 + continue + if not twole_folder.exists(): + twole_folder = base_dir / "二一教育" + if not twole_folder.exists(): print("❌ 文件夹不存在,请先解压文件") # 将失败的原始压缩包拷贝到本程序所在目录的 ee 子目录 script_dir = Path(__file__).parent