{"id":26045065,"url":"https://github.com/poboll/webprint","last_synced_at":"2026-04-19T17:38:18.345Z","repository":{"id":280739427,"uuid":"942999217","full_name":"poboll/webprint","owner":"poboll","description":"Python 实现网络打印机：Flask + win32print","archived":false,"fork":false,"pushed_at":"2025-03-05T03:12:49.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-05T04:20:55.511Z","etag":null,"topics":["flask","python3","win32print"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/poboll.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-05T02:41:06.000Z","updated_at":"2025-03-05T03:12:52.000Z","dependencies_parsed_at":"2025-03-05T04:32:20.516Z","dependency_job_id":null,"html_url":"https://github.com/poboll/webprint","commit_stats":null,"previous_names":["poboll/webprint"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poboll%2Fwebprint","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poboll%2Fwebprint/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poboll%2Fwebprint/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poboll%2Fwebprint/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/poboll","download_url":"https://codeload.github.com/poboll/webprint/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242450623,"owners_count":20130212,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["flask","python3","win32print"],"created_at":"2025-03-07T19:31:06.721Z","updated_at":"2025-12-05T01:03:02.008Z","avatar_url":"https://github.com/poboll.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WebPrint\n\n基于 Flask + Python + win32print 的简易网页打印服务\n\n## 项目简介\n\n`WebPrint` 是一个可在 Windows 环境下，通过 Flask 提供 Web 界面上传 `doc` / `docx` / `pdf` 文件，并调用本地安装的 **HP LaserJet P1007** 打印机（也可自行改成其他打印机）进行打印的简易项目。\n\n本项目适用于以下场景：\n- 办公室环境：无需专业文档管理，只想简单通过局域网网页上传并自动打印\n- 自助打印：在局域网中让员工/客户直接上传文档并打印\n\n![](https://github.com/user-attachments/assets/bb18e927-55fd-4f12-a203-bde3c91b5a42)\n![](https://github.com/user-attachments/assets/8ffa78bb-bd63-4b2e-a2c2-524c30864bf3)\n\n\u003e **注意**：只能在 **Windows** 环境下使用，因为依赖 `pywin32` 库（win32api、win32print 等）进行打印操作。\n\n## 功能特性\n\n1. 上传文件格式支持：`doc`, `docx`, `pdf`  \n2. 自动调用指定打印机（如 `HP LaserJet P1007`）打印  \n3. 使用 `Flask` 提供 Web 界面，简单易用  \n4. 基本安全处理，包括文件名过滤、上传限制等  \n\n## 运行环境\n\n- Windows (具有安装并可正常使用的打印机，如 HP LaserJet P1007)\n- Python 3.7+（测试环境为 Python 3.8 / 3.9 / 3.10 均可）\n- 需要安装以下依赖：\n  - `flask`\n  - `pywin32` (win32api, win32print)\n  - （可根据需要安装 `werkzeug` 等最新版本，一般 `pip install Flask pywin32` 即可）\n\n## 安装与使用\n\n1. **克隆本项目**（或者下载 ZIP 解压）\n   ```bash\n   git clone https://github.com/poboll/webprint.git\n   cd webprint\n   ```\n\n2. **安装依赖**\n   ```bash\n   pip install -r requirements.txt\n   ```\n   如果你没有 `requirements.txt`，可自行安装：\n   ```bash\n   pip install flask pywin32\n   ```\n\n3. **在 Windows 系统中确保安装了你要使用的打印机驱动**  \n   - 并在脚本中修改 `print_file_hp_p1007` 函数，改成你的打印机名字或打印机匹配关键字。\n\n4. **启动服务**\n   ```bash\n   python app.py\n   ```\n   默认会在 `http://0.0.0.0:7001` 监听，你也可以在 `app.run()` 中修改端口。\n\n5. **访问 Web 界面**  \n   - 在浏览器中输入 `http://你的服务器IP:7001/` 即可访问上传页面\n   - 选择要打印的 `doc/docx/pdf` 文件后提交\n\n## 注意事项\n\n- 如果你在调用 `ShellExecute(printto, ...)` 时报错：`pywintypes.error: (31, 'ShellExecute', '连到系统上的设备没有发挥作用。')`，通常是因为你没有给文件类型配置默认打开程序。  \n  - 例如 `.pdf` 必须与你的 PDF 阅读器（Adobe 或其他）建立默认关联，`.docx` 必须和 Office Word 建立关联，否则无法执行 `printto`。\n\n- 打印机名称匹配失败的话，可以在函数 `print_file_hp_p1007` 里把关键字改成你自己的打印机名字（如 `\"HP LaserJet P1108\"`），或者不做大小写匹配。\n\n- **安全**：本项目仅做简单演示，若要用于生产环境，需进行访问权限控制、上传大小限制、文件类型更严格校验等措施。\n\n## 文件结构\n\n```\nwebprint/\n  ├── app.py                 # Flask 主程序\n  ├── templates/\n  │    └── index.html        # 上传界面\n  ├── temple/                # 临时存放上传的文件\n  ├── requirements.txt       # Python依赖\n  └── README.md              # 说明文档\n```\n\n## 常见问题\n\n1. **为什么打印机不执行任务？**  \n   - 确认 `win32print.SetDefaultPrinter` 设置正确的打印机  \n   - 确认本机 Office / PDF 阅读器能默认打开对应文件  \n   - 确认 Windows 打印服务正常启用\n\n2. **如何换别的打印机？**  \n   - 修改 `if 'HP LASERJET P1007' in prn.upper():` 那一行即可。\n\n3. **如何提供外网访问？**  \n   - 在 `app.run()` 使用 `host='0.0.0.0'` 并做好路由器端口映射或云服务器安全组开放端口即可。\n\n---\n\n## 三、简单原理说明\n\n- **Flask 部分**  \n  通过一个简单的表单（`index.html`）上传文件到后端 (`upload_print_file`)，后端会先做安全校验（检查文件后缀、生成安全文件名等），再调用 `print_file_hp_p1007` 打印。\n\n- **win32print \u0026 win32api**  \n  - `win32print.EnumPrinters` 用于获取本地打印机列表  \n  - `win32print.SetDefaultPrinter` 设置默认打印机  \n  - `win32api.ShellExecute(…, “printto”, …)` 可调用已关联应用的 “打印到” 功能，必须指定打印机名称。  \n  - 前提：Windows 对应文件类型 (doc, docx, pdf) 有默认关联应用才能正常 “printto”\n\n- **安全注意**  \n  项目中通过 `secure_filename`、时间戳重命名等方式，防止恶意上传脚本并执行等安全风险。但**仍不建议公开给 Internet**，最好只在内网使用并配合身份鉴权。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpoboll%2Fwebprint","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpoboll%2Fwebprint","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpoboll%2Fwebprint/lists"}