python - Win32Com:添加一个新的 Excel 文件并另存为 .xlsm
问题描述
我在 Python 中使用 Win32com。
基本上我想创建一个新的 .xlsm 工作簿。
1)但是SaveAs对我不起作用,我总是遇到以下错误
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "Microsoft Excel cannot access the file 'C:\\//Users/~/322DF100'. There are several possible reasons:\n\n• The file name or path does not exist.\n• The file is being used by another program.\n• The workbook you are trying to save has the same name as a currently open workbook.", 'xlmain11.chm', 0, -2146827284), None)
import win32com.client as w3c
xl=w3c.DispatchEx("Excel.Application")
xl.DisplayAlerts=False
wb = xl.Workbooks.Add()
wb.SaveAs ('C:/Users/~/OUTPUT8.xlsm')
wb.Close(SaveChanges=False)
del wb
xl.Application.Quit()
del xl
2) SaveCopyAs 有效,但我无法打开文件
Excel 无法打开文件,因为文件格式或扩展名无效。
import win32com.client as w3c
xl=w3c.DispatchEx("Excel.Application")
xl.DisplayAlerts=False
wb = xl.Workbooks.Add()
wb.SaveCopyAs ('C:/Users/~/OUTPUT8.xlsm')
wb.Close(SaveChanges=False)
del wb
xl.Application.Quit()
del xl
解决方案
推荐阅读
- mysql - MySql 8删除限制子查询
- c# - 使用 Active Directory 和 Windows 身份验证在 Blazor Server 中提供自定义角色
- json - 通过 ajv-cli 在 $ref 中使用 URI
- manifest - Redemption64.dll 也没有清单
- html - 如何使我的 CSS 动画具有响应性?
- vue.js - Electron 桌面应用和 Vue.js CSP 设置
- php - Wordpress:functions.php 或插件之外的函数
- azure-devops - 通过 Azure DevOps 中的 Graph API 设置库安全角色
- php - Codeigniter 更新表中的值
- sql - 如何在现有 SQL Server 查询中列出外键及其引用列