Excel访问被拒绝,使用Win32 Python Pywin32。

10

我的代码是

#Opens template for creating final report
excel = win32.dynamic.Dispatch('Excel.Application')
template = os.path.abspath((folderpath+'\Poop.xlsx'))
wb = excel.Workbooks.Open(template)
freshws= wb.Sheets("Fresh") #Sheet names must match perfectly
secws= wb.Sheets("sec")

cur.execute("Select * from FIRALL")
freshdata=list(cur.fetchall())
#writes to the first sheet
datarowlen=0
for i,a in enumerate(freshdata):
    datarowlen = len(a)
    for j,b in enumerate(a):
        freshws.Cells(i+1,j+1).Value = a[j]

cur.execute("Select * from SECVE")
secdata=list(cur.fetchall())
#writes to the second sheet
datarowlen=0
for i,a in enumerate(secdata):
    datarowlen = len(a)
    for j,b in enumerate(a):
        secws.Cells(i+1,j+1).Value = a[j]
#saves the report
wb.SaveAs()
wb.Close()

运行代码时出现的错误是

Traceback (most recent call last):
  File "main.py", line 369, in <module>
    wb = excel.Workbooks.Open(template)
  File "<COMObject <unknown>>", line 8, in Open
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel'
, "Microsoft Excel cannot access the file 'path to stuff------------------------
Poop Report\\Poop.xlsx'. There are several possible reasons:\n\n\u2022 The file
name or path does not exist.\n\u2022 The file is being used by another program.\
n\u2022 The workbook you are trying to save has the same name as a currently ope
n workbook.", 'xlmain11.chm', 0, -2146827284), None)

我收到一个弹出对话框,显示访问被拒绝。该文件不是只读的,并且我是要打开的工作簿的所有者。我已经尝试过

win32.gencache.EnsureDispatch('Excel.Application')

我仍然得到同样的错误。我有什么遗漏的吗?我尝试使用动态思维来解决此错误,但是未成功。

另一个错误是在我尝试修复此代码时出现了Pywins -2147418111错误。


你的 folderpath 变量是从哪里来的?因为看起来你正在将路径 'path to stuff------------------------ Poop Report\\Poop.xlsx' 传递给 Excel,这显然不是一个有效的路径,这就是为什么你会得到那个错误的原因。 - abarnert
我只是把它放在那里来隐藏路径,路径不是问题,因为我已经使用openpyxt没有问题,但它无法处理数据透视表或更新它们。 - Drfrink
3个回答

16

我和一个同事正在诊断这个确切的问题。我简直不敢相信这是多么隐晦,我们通过搜索与.NET等效代码类似的问题找到了解决方案:

要解决此问题,请在64位体系结构上的'C:\Windows\SysWOW64\config\systemprofile\'中或32位服务器上的'C:\Windows\System32\config\systemprofile\'中创建名为“Desktop”的文件夹。

这真正地解决了完全相同的问题。


2
我简直不敢相信这就是答案。感谢 Stack Overflow,感谢 Matthew。我现在不知道是该笑还是该哭了。 - erik-sn
1
这个有效,但为什么呢?为什么Windows会有如此随机的错误?这些目录会如何发挥作用?你是如何找出来的? - Tom

5
我最终解决了问题,原因不明,以下方法可行。如果有人能够评论一下为什么这样做有效,我将不胜感激。
主要更改的是打开工作簿时路径中斜杠“/”改成了反斜杠“\”。
然后我无法选择工作表名称,直到我让Excel可见。
excel.Visible = True
wb = excel.Workbooks.Open((excelreport+"\Poop.xlsx"))

奇怪的是,这消除了pywins错误。

还更改了如何填充表格,现在是:

cur.execute("Select * from FIRALL")
freshdata=list(cur.fetchall())
#writes to the first sheet
freshws.Range(freshws.Cells(2,1),freshws.Cells((len(freshdata)+1),len(freshdata[0]))).Value = freshdata

希望这篇文章能帮助到其他遇到同样问题的人。

即使 Excel 文件具有正确的写入访问权限,由于速度慢,仍可能出现此错误。 我猜问题可能是因为我们的 Excel 应用程序在使用 win32 模块时响应速度较慢,在执行 SaveAs 之前,让代码休眠 5 秒钟帮助我解决了这个问题。 - Prakash

1
为了解决这个问题,请按照以下步骤操作: 在运行中打开DCOMCNFG, 展开组件服务 > 我的电脑 > DCOM配置,找到Microsoft Excel应用程序 > 属性(右键单击)- > 在身份选项卡下将其设置为“交互式用户以进行后台服务”。

enter image description here

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接