写入内容到本地加密/受保护的文件,支持企业安全策略环境。支持文本文件、Word 文档 (.docx)、Excel 表格 (.xlsx) 等 80+ 种格式,通过正确的编码处理避免乱码问题。
写入内容到本地加密/受保护的文件,支持企业安全策略环境
当用户提到以下关键词时激活:
# 覆盖写入
python D:\ai\workspace\skills\encrypted-file-writer\write_file.py "文件路径" "内容"
# 追加写入
python D:\ai\workspace\skills\encrypted-file-writer\write_file.py "文件路径" "内容" --append
# 从标准输入读取
echo "内容" | python D:\ai\workspace\skills\encrypted-file-writer\write_file.py "文件路径" --stdin
# 指定编码
python D:\ai\workspace\skills\encrypted-file-writer\write_file.py "文件路径" "内容" --encoding gbk
# 写入文本文件
python D:\ai\workspace\skills\encrypted-file-writer\write_file.py "E:\data\test.txt" "Hello World"
# 写入日志(追加模式)
python D:\ai\workspace\skills\encrypted-file-writer\write_file.py "E:\logs\app.log" "2026-03-08 10:00:00 - 系统启动" --append
# 写入代码文件
python D:\ai\workspace\skills\encrypted-file-writer\write_file.py "D:\project\main.py" "print('Hello')"
# 写入配置文件
python D:\ai\workspace\skills\encrypted-file-writer\write_file.py "D:\config\app.json" "{\"name\": \"test\"}"
# 写入 Word 文档
python D:\ai\workspace\skills\encrypted-file-writer\write_file.py "E:\docs\report.docx" "第一行内容"
# 追加到 Word 文档
python D:\ai\workspace\skills\encrypted-file-writer\write_file.py "E:\docs\report.docx" "追加内容" --append
# 写入 Excel 文件(CSV 格式)
python D:\ai\workspace\skills\encrypted-file-writer\write_file.py "E:\data\data.xlsx" "姓名,年龄,城市
张三,25,北京
李四,30,上海"
# 从管道写入
echo "多行内容" | python D:\ai\workspace\skills\encrypted-file-writer\write_file.py "E:\data\input.txt" --stdin
# 写入文件并获取结果
exec:
command: python D:\ai\workspace\skills\encrypted-file-writer\write_file.py "文件路径" "内容"
| 类型 | 扩展名 | 写入方式 |
|---|---|---|
| 文本类 | .txt, .md, .markdown, .rst, .log, .csv, .tsv | UTF-8 文本写入 |
| 代码类 | .java, .py, .js, .ts, .jsx, .tsx, .c, .cpp, .h, .cs, .go, .rs, .rb, .php, .vue | UTF-8 文本写入 |
| 配置类 | .json, .xml, .yaml, .yml, .toml, .ini, .cfg, .properties, .gradle, .config, .env | UTF-8 文本写入 |
| 样式类 | .html, .htm, .css, .scss, .sass, .less | UTF-8 文本写入 |
| 脚本类 | .sh, .bash, .bat, .cmd, .ps1, .sql | UTF-8 文本写入 |
| Office | .docx (Word), .xlsx (Excel) | OpenXML ZIP 格式写入 |
| 模式 | 参数 | 说明 |
|---|---|---|
| 覆盖写入 | (默认) | 如果文件存在则覆盖,不存在则创建 |
| 追加写入 | --append 或 -a | 在文件末尾追加内容 |
| 编码 | 参数 | 适用场景 |
|---|---|---|
| UTF-8 | (默认) | 推荐,国际通用编码 |
| GBK | --encoding gbk | 中文 Windows 系统兼容 |
| GB2312 | --encoding gb2312 | 简体中文旧文件 |
| Latin-1 | --encoding latin1 | 西欧语言文件 |
| 文件类型 | 处理方式 |
|---|---|
| 文本文件 | 内容先编码为 UTF-8 字节,再以二进制模式写入,避免编码转换问题 |
| Word (.docx) | 使用 zipfile 创建/修改 OpenXML 格式,在 document.xml 中添加段落 |
| Excel (.xlsx) | 使用 zipfile 创建/修改 OpenXML 格式,生成 sharedStrings 和 worksheet |
</w:body> 前插入新段落,重新打包成功输出:
[OK] 成功写入 XXX 字节到:文件路径
FILE_PATH=文件路径
BYTES_WRITTEN=字节数
失败输出:
[ERROR] 错误信息
python write_file.py "E:\logs\app.log" "[INFO] 操作完成" --append
python write_file.py "D:\config\settings.json" "{\"debug\": true}"
python write_file.py "D:\project\output.py" "def main():\n print('Hello')"
# 创建新文档
python write_file.py "E:\docs\report.docx" "报告标题
报告内容第一行
报告内容第二行"
# 追加到现有文档
python write_file.py "E:\docs\report.docx" "新增段落" --append
python write_file.py "E:\exports\data.xlsx" "姓名,年龄,城市
张三,25,北京
李四,30,上海"
python write_file.py "C:\temp\task_123.txt" "任务数据..."
⚠️ 重要说明:
--encoding 参数⚠️ Office 文件限制:
# 读取 Word 文档
python D:\ai\workspace\skills\encrypted-file-reader\read_file.py "E:\docs\report.docx"
# 修改内容后写回
python D:\ai\workspace\skills\encrypted-file-writer\write_file.py "E:\docs\report.docx" "新内容" --append
| 版本 | 日期 | 变更 |
|---|---|---|
| 1.0.0 | 2026-03-08 | 初始版本,支持文本/代码/配置文件写入,UTF-8 编码保护 |
| 1.1.0 | 2026-03-09 | 新增 .docx 和 .xlsx 写入支持,与 encrypted-file-reader 格式保持一致 |
ZIP package — ready to use