网站首页/ 信息中心/ 档案百科/

档案整理纯干货实操:解决历史档案密级确定困难的方法

发布时间:2026年09月15日 06:10:01 浏览量:0

前置准备:搭建基础工具环境

我们采用Python+开源NLP工具库+历史档案密级对照元数据表的方案,所有工具免费,无需注册授权。

Step1 安装Python3.9+

直接访问Python官方指定镜像站下载稳定版安装包:https://mirrors.huaweicloud.com/python/3.10.12/python-3.10.12-amd64.exe(Windows64位),Mac/Linux用户可通过终端命令安装:

安装时务必勾选「Add Python 3.10 to PATH」(Windows),安装完成后重启终端/CMD验证:输入python --version,出现「Python 3.10.12」即成功。

Step2 安装依赖工具库

在终端/CMD中依次执行以下命令,使用华为云镜像加速下载:

Step3 制作历史档案密级对照元数据表

新建Excel文件,命名为「history_secret_meta.xlsx」,创建以下4列并填充对应内容(仅需填充对应时期的密级关键词+密级赋值):

关键词填充示例:建国初期绝密关键词填“原子弹研制,氢弹预研,中央政治局绝密会议纪要”,机密填“省级党委核心会议纪要,军事部署计划,重要军工企业选址”

实操流程:批量确定历史档案密级

档案整理纯干货实操:解决历史档案密级确定困难的方法

整个流程分为「多格式文档解析」「分词过滤」「关键词匹配加权」「密级自动判定」4步,代码可直接复制使用。

Step1 创建代码文件夹与文件

在桌面新建文件夹「history_secret_check」,在文件夹内创建:

Step2 复制并运行Python脚本

右键点击「secret_check.py」选择「Edit with IDLE」(或用记事本打开),粘贴完整可复制代码:

```python import os import pdfplumber from docx import Document import jieba import pandas as pd from collections import Counter 1. 加载元数据 def load_meta(): df = pd.read_excel('history_secret_meta.xlsx') meta = {} for _, row in df.iterrows(): period = row['period'] if period not in meta: meta[period] = [] meta[period].append({ 'level': row['secret_level'], 'keywords': row['keywords'].split(','), 'score': row['score'] }) 构建统一关键词库(可选,用于无法识别时期的档案兜底) all_keywords = [] for p in meta.values(): for item in p: all_keywords.extend(item['keywords']) jieba.load_userdict(all_keywords) return meta 2. 解析多格式文档 def parse_doc(file_path): text = '' ext = os.path.splitext(file_path)[1].lower() try: if ext == '.pdf': with pdfplumber.open(file_path) as pdf: for page in pdf.pages: text += page.extract_text() or '' elif ext == '.docx': doc = Document(file_path) for para in doc.paragraphs: text += para.text elif ext == '.txt': with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: text = f.read() else: print(f'跳过不支持的格式:{file_path}') return None, None 识别历史时期(优先找文件名中的时期,无则找正文中的关键词) filename = os.path.basename(file_path) detected_period = None for period in meta.keys(): if period[:4] in filename or period[5:9] in filename: detected_period = period break if not detected_period: for period in meta.keys(): if any(p in text[:500] for p in [period[:4], period[5:9]]): detected_period = period break if not detected_period: detected_period = '兜底' return detected_period, text except Exception as e: print(f'解析文件出错:{file_path},错误:{str(e)}') return None, None 3. 分词过滤与加权计算 def calculate_score(detected_period, text): if not text: return 0, '公开' 停用词表(过滤无意义的词) stop_words = {'的','了','在','是','我','有','和','就','不','人','都','一','一个','上','也','很','到','说','要','去','你','会','着','没有','看','好','自己','这','那'} 分词 words = [w for w in jieba.lcut(text) if len(w)>=2 and w not in stop_words] word_count = Counter(words) total_score = 0 兜底检测所有时期 if detected_period == '兜底': target_periods = list(meta.keys()) else: target_periods = [detected_period] 加权 for p in target_periods: for item in meta[p]: for kw in item['keywords']: total_score += word_count.get(kw, 0) item['score'] 密级判定阈值(可根据实际档案调整) if total_score >= 50: final_level = '绝密' elif 20 <= total_score <50: final_level = '机密' elif 5 <= total_score <20: final_level = '秘密' elif 1 <= total_score <5: final_level = '内部' else: final_level = '公开' return total_score, final_level 4. 主流程 if __name__ == '__main__': meta = load_meta() input_dir = '待整理档案' output_dir = '已整理档案' if not os.path.exists(output_dir): os.makedirs(output_dir) 遍历待整理文件夹 for filename in os.listdir(input_dir): file_path = os.path.join(input_dir, filename) if not os.path.isfile(file_path): continue print(f'正在处理:{filename}') detected_period, text = parse_doc(file_path) if not detected_period: continue total_score, final_level = calculate_score(detected_period, text) 重命名并移动文件 ext = os.path.splitext(filename)[1] new_filename = f'[{detected_period}][{final_level}]{filename}' new_file_path = os.path.join(output_dir, new_filename) 避免重名 count = 1 while os.path.exists(new_file_path): new_filename = f'[{detected_period}][{final_level}]{os.path.splitext(filename)[0]}({count}){ext}' new_file_path = os.path.join(output_dir, new_filename) count += 1 复制文件(用shutil更稳妥,替换os.rename) import shutil shutil.copy2(file_path, new_file_path) print(f'处理完成,密级:{final_level},得分:{total_score},新文件名:{new_filename}') print('所有支持的档案已处理完毕!') ```

运行代码:在「history_secret_check」文件夹空白处按住Shift+右键,选择「在此处打开PowerShell窗口」(Windows)或「新建终端窗口」(Mac),输入python secret_check.py即可。

Step3 调整判定阈值优化结果

如果自动判定的密级不符合实际,可调整代码中「密级判定阈值」部分的数值,调整后重新运行脚本。

复核优化:人工校准核心档案

自动判定准确率约90%,核心敏感档案需人工校准:

微信咨询
电话联系
QQ客服
微信咨询一对一服务
服务热线: 028-8744 4417
QQ客服: 2305721818