excel访问被win32 python pywin32拒绝 [英] excel access denied with win32 python pywin32

查看:133
本文介绍了excel访问被win32 python pywin32拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是

#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()

运行代码时出现的错误是

The error i get when I run my code is

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)

我看到一个弹出对话框,提示访问被拒绝.该文件不是只读文件,我是其尝试打开的工作簿的所有者.我已经尝试过

I get a popup dialog saying access is denied. The file isn't readonly and I'm the owner of the workbook its trying to open. I've tried

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

我仍然遇到相同的错误.有什么我想念的吗?我转向动态思维,认为后期绑定可以解决此错误.

I still get the same error. Is there something I'm missing? I switched to dynamic thinking late-binding would solve this error.

当我尝试修复此代码时,我遇到的另一个错误是Pywins -2147418111错误.

another error I had was Pywins -2147418111 error when I was trying to fix this code.

推荐答案

由于某些原因,如果有人可以评论我对此表示感谢的原因,我最终将其修复.

I ended up fixing it for some reason this works, if someone could comment why I would appreciate that.

我打开工作簿所做的主要更改是路径中从/到\的斜线.

Main thing I changed to open the workbook was the slashes from / to \ in the pathways.

然后,在我使Excel可见之前,我无法选择工作表名称.

Then I couldn't select the sheet name until I made excel visible.

excel.Visible = True
wb = excel.Workbooks.Open((excelreport+"\Poop.xlsx"))

很奇怪,它消除了pywins错误

Oddly enough that got rid of the pywins error

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

Also changed how sheets are filled its now

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

希望这可以帮助遇到我遇到的相同问题的其他人.

Hopefully this helps anyone else who runs into the same issues I did.

这篇关于excel访问被win32 python pywin32拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆