要解决数字档案馆系统繁琐的点击、录入、上传流程,最直接的方法是构建一个基于Python的自动化中间层。这一层将模拟人工操作,把复杂的GUI交互转化为后台指令。首先需要在本地搭建Python运行环境。
请直接访问Python官网下载 Python 3.10.7 或更高版本的Windows安装包(64位)。安装时,务必勾选界面底部的 "Add Python to PATH" 选项,这是后续命令行调用的关键。
安装完成后,打开命令提示符(CMD),输入以下命令安装自动化所需的依赖库。这里我们使用Selenium进行浏览器控制,使用WebDriver Manager自动管理浏览器驱动,避免因驱动版本不匹配导致的卡壳。
```bash pip install selenium webdriver-manager pyinstaller ```执行完毕后,输入 `python --version` 和 `pip list` 确认环境无误。这一步完成后,我们就具备了接管浏览器操作的能力。
在编写代码前,必须对现有的数字档案馆系统进行“逆向工程”。我们的目标是找出那些重复性高、价值低的操作节点。通常包括:登录验证、多级菜单点击、元数据手动填写、文件选择对话框。
打开Chrome浏览器,进入数字档案馆系统登录页。按下 F12 键打开开发者工具,点击左上角的“箭头图标”或使用快捷键 Ctrl+Shift+C 开启元素检查模式。
将鼠标移动到用户名输入框,高亮显示的HTML代码即为定位依据。我们需要记录以下关键信息,后续代码将依赖这些定位器(Locator):
请将这些信息记录在记事本中,代码中会直接使用这些字符串来“告诉”脚本点哪里。

在本地新建一个文件夹,命名为 `AutoArchiver`。在该文件夹内新建一个文本文件,重命名为 `archive_bot.py`。将以下完整代码复制进去。这段代码封装了登录、元数据填充、文件上传的全过程。
```python import os import time from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from webdriver_manager.chrome import ChromeDriverManager class DigitalArchiveBot: def __init__(self, login_url, username, password): 初始化浏览器配置,设置为无头模式可根据需求开启,这里为了演示保留界面 options = webdriver.ChromeOptions() options.add_experimental_option("excludeSwitches", ["enable-logging"]) 允许在非安全上下文上传文件(部分老系统需要) prefs = {"profile.default_content_settings.popups": 0, "download.default_directory": os.getcwd()} options.add_experimental_option("prefs", prefs) self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) self.login_url = login_url self.username = username self.password = password self.wait = WebDriverWait(self.driver, 10) def login(self): """执行自动登录""" print(f"正在访问: {self.login_url}") self.driver.get(self.login_url) 显式等待用户名框出现 user_input = self.wait.until(EC.presence_of_element_located((By.ID, "username"))) user_input.send_keys(self.username) 定位密码框并输入 pass_input = self.driver.find_element(By.ID, "password") pass_input.send_keys(self.password) 点击登录按钮 login_btn = self.driver.find_element(By.XPATH, "//button[@type='submit']") login_btn.click() 等待登录成功后的特征元素出现,例如“首页”或“控制台”字样 print("等待登录验证...") self.wait.until(EC.title_contains("数字档案馆")) print("登录成功!") def upload_single_file(self, file_path, title, category): """ 上传单个文件的核心逻辑 :param file_path: 本地文件绝对路径 :param title: 档案题名 :param category: 档案分类 """ try: print(f"正在处理: {title}") 1. 导航到归档页面 (假设URL为 /archive/upload,请根据实际情况修改) self.driver.get(f"{self.login_url}/archive/upload") time.sleep(1) 简单等待页面渲染 2. 填写元数据:题名 title_input = self.wait.until(EC.presence_of_element_located((By.ID, "archiveTitle"))) title_input.clear() title_input.send_keys(title) 3. 填写元数据:分类 (假设是下拉框,此处模拟点击) category_select = self.driver.find_element(By.ID, "categorySelect") category_select.click() time.sleep(0.5) 选中对应的分类选项 category_option = self.driver.find_element(By.XPATH, f"//li[text()='{category}']") category_option.click() 4. 核心技巧:文件上传 很多系统的上传按钮是经过JS封装的,直接点击会弹窗。 我们必须找到页面中隐藏的 input type='file' 元素。 file_input = self.driver.find_element(By.XPATH, "//input[@type='file']") file_input.send_keys(os.path.abspath(file_path)) 5. 提交表单 submit_btn = self.driver.find_element(By.ID, "submitBtn") submit_btn.click() 6. 等待提交成功的提示框 self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, "success-message"))) print(f"文件 {title} 归档完成。") 返回列表页或重置,准备下一个 time.sleep(1) except Exception as e: print(f"处理文件 {title} 时出错: {str(e)}") 截图保存错误现场 self.driver.save_screenshot(f"error_{title}.png") def close(self): self.driver.quit() if __name__ == "__main__": ================= 配置区域 ================= 请将此处替换为第二步中记录的真实信息 TARGET_URL = "http://192.168.1.100:8080/login" USER = "admin" PASS = "your_secure_password" 初始化机器人 bot = DigitalArchiveBot(TARGET_URL, USER, PASS) try: bot.login() 模拟一个待上传的任务列表 实际场景中,你可以通过读取Excel或遍历文件夹来生成这个列表 tasks = [ {"file": "test_doc1.pdf", "title": "2023年度财务报表", "category": "财务档案"}, {"file": "test_doc2.pdf", "title": "项目立项申请书", "category": "文书档案"}, ] for task in tasks: 确保文件存在 if os.path.exists(task["file"]): bot.upload_single_file(task["file"], task["title"], task["category"]) else: print(f"文件未找到: {task['file']}") finally: bot.close() ```请注意代码中的 “配置区域” 和 元素定位部分(如 `By.ID, "archiveTitle"`)。你需要将你在第二步中获取的真实ID和XPath替换掉代码中的示例值。这是脚本能否运行的关键分水岭。
单文件自动化只是开始,真正的提效在于批量处理。我们需要修改 `if __name__ == "__main__":` 下的逻辑,让脚本自动扫描一个本地文件夹,并将文件名自动映射为“档案题名”。
在 `archive_bot.py` 的同级目录下新建一个 `batch_upload` 文件夹,放入若干个PDF文件。将主程序逻辑替换为以下代码:
```python ... bot.login() 之前的代码保持不变 ... try: bot.login() 设定批量扫描的文件夹路径 source_dir = "./batch_upload" 遍历文件夹 for filename in os.listdir(source_dir): if filename.endswith(".pdf") or filename.endswith(".jpg"): file_path = os.path.join(source_dir, filename) 简单的元数据提取逻辑:文件名去掉后缀作为题名 例如 "2023会议记录.pdf" -> 题名: "2023会议记录" archive_title = os.path.splitext(filename)[0] 默认分类,可根据文件名包含的关键字进行if-else判断 archive_category = "通用档案" if "合同" in filename: archive_category = "合同档案" elif "人事" in filename: archive_category = "人事档案" print(f"开始自动归档: {filename} -> {archive_title}") bot.upload_single_file(file_path, archive_title, archive_category) finally: bot.close() ```通过这段逻辑,你只需要将文件拖入 `batch_upload` 文件夹,并按照“分类+关键词”的规则命名文件,脚本即可自动完成数百份文件的归档,无需人工干预每一个字段。
为了让不懂技术的同事也能使用这个简化工具,我们需要将Python脚本打包为 `.exe` 可执行文件。这样他们无需安装Python环境即可双击运行。
在CMD中切换到 `AutoArchiver` 目录,执行以下打包命令。`--onefile` 参数表示将所有依赖打包成一个单独的exe文件,`--noconsole` 参数表示运行时不显示黑色命令框(如需调试过程可去掉此参数)。
```bash pyinstaller --onefile --noconsole archive_bot.py ```等待打包完成后,在目录下的 `dist` 文件夹中会生成 `archive_bot.exe`。你可以将这个exe文件和 `batch_upload` 文件夹一起发送给业务人员。他们只需将待归档文件放入文件夹,双击exe,即可在后台自动完成原本需要耗时数天的系统录入工作。
通过以上五个步骤,我们利用Selenium自动化技术,在用户与复杂的数字档案馆系统之间建立了一个透明的“简化层”,彻底解决了操作繁琐、重复录入多、易出错等痛点。