python - 在 Python 中使用 smbclient 保存 Excel 文件
问题描述
在使用 openpyxl 执行一些工作簿操作后,我一直在尝试覆盖 Excel 文件。由于使用相关文件需要访问权限,因此正在使用 smbclient 来访问它。
尝试使用 openpyxl 对象的内置save()
方法保存文件的新版本时会出现问题,根据文档,该save()
方法涉及打开文件以完成操作:
>>> excel_file.save('\path\to\my\file.xlsx')
PermissionError: [Errno 13] Permission denied: '\path\to\my\file.xlsx'
由于文件受到限制,因此需要with
在 smbclient 打开文件的块内执行操作。但是,这会导致抛出相同的错误,因为该save()
方法无法再打开文件以完成操作,因为文件已经使用 smbclient 打开(文件被阻止):
with smbclient.open_file('\path\to\my\file.xlsx', mode='rb') as s:
xlsx_file.save(s.name)
PermissionError: [Errno 13] Permission denied: '\path\to\my\file.xlsx'
在这里,s.name
只是获取打开文件对象的路径属性。
有谁知道我如何解决这个问题?
解决方案
我使用此连接,这对我有用:
with smbclient.open_file('\path\to\my\file.xlsx', mode='rb') as s
xlsx_file.to_excel(s)
推荐阅读
- airflow - 如何使用 Celery 配置 Apache Airflow 以运行并发任务?
- android - 重新启用滚动后,水平 RecyclerView 不会向左滚动
- django - 如何从 Django 中的表单安全地更新模型
- javascript - 在同一 HTML 中的 PHP 和 JS 之间传递变量
- javascript - 包含字符串和数字的两个数组之间的区别
- http - 如何通过 HTTP 请求提供 PDF 文件
- javascript - 使用网络摄像头扫描二维码,从 wencam 获取图像
- salesforce - 原因 -nsert 失败。第 1 行的第一个例外;第一个错误:INVALID_CROSS_REFERENCE_KEY,无效的交叉引用 ID:
- python - 错误消息:超出 IOPub 数据速率。从列表中提取字符串时
- c# - 如何优先考虑应该首先搜索哪个过滤器的 linq 查询?