[python-win32] pywin can not access excel file

Steven Manross steven at manross.net
Sun Dec 12 20:46:21 EST 2021


P.S. I also just validated that a non-admin could run the code in ipython with a test xlsx, and open the workbook, navigate to a worksheet, and then read a value in A1 that I put the words "this is a test" in.


  *   Is User Account Control (UAC) enabled on the PC we are troubleshooting with?  That could be causing issues and you may need to troubleshoot that.
  *   Does the non-admin have NTFS permissions to the Excel workbook you are working with (my test was just c:\\scripts\\test.xlsx)?  (Can the non-admin browse to that file and double click it to open it - just as a test?)
  *   What OS is the PC that we are trying to troubleshoot (W11, W10, W7, W8? Or possibly terminal services on a Server OS?) My test was on W10 x64 build 20H2
  *   What version of pywin32 are you running and what version of python are we troubleshooting?  Im running Python 3.6

my pywin32 version:

C:\python36>pip freeze| findstr /c:pywin32
WARNING: Ignoring invalid distribution -ffi (c:\python36\lib\site-packages)
pywin32==302
pywin32-ctypes==0.1.2

My guess is that this is more an environmental issue than a python problem, but the version info might help in diagnosing this further.

# my test code
import win32com.client as w3c

filename = 'C:\\\\scripts\\\\this.xlsx'

ExcelApp = w3c.DispatchEx("Excel.Application")
ExcelApp.Visible = False
ExcelApp.DisplayAlerts = False
wb = ExcelApp.Workbooks.Open("{0}".format(filename))
worksheet = wb.Worksheets[0]
worksheet.Range("A1").Value

# Ipython output:

In [5]: worksheet.Range("A1").Value
Out[5]: 'this is a test'


P.S. Im also a fan of F-strings...

      wb = ExcelApp.Workbooks.Open(f"{filename}")

OR more simply

      wb = ExcelApp.Workbooks.Open(filename)

HTH

Steven
From: python-win32 <python-win32-bounces+steven=manross.net at python.org> On Behalf Of Steven Manross
Sent: Sunday, December 12, 2021 6:00 PM
To: Tamara Abugharbieh <Tamara.Abugharbieh at Realsoft-me.com>; python-win32 at python.org
Subject: Re: [python-win32] pywin can not access excel file

Is this a typo on the email or is your production code really referencing filename and fileName?

You need to reference the variable name using the same casing in both places (if this is not a typo in your email example).

P.S. I don't think you need the 4 backslashes..  2 usually does the trick, but there's nothing from stopping it from working using 4 backslashes.


import win32com.client as w3c



# lowercase

filename = 'C:\\\\App\\\\Backend\\\\public\\\\MSB_2021\\\\11\\\\SYB_10_7_V1.xlsx'

ExcelApp = w3c.DispatchEx("Excel.Application")
ExcelApp.Visible = False
ExcelApp.DisplayAlerts = False
# mixed case
wb = ExcelApp.Workbooks.Open("{0}".format(fileName))


From: python-win32 <python-win32-bounces+steven=manross.net at python.org> On Behalf Of Tamara Abugharbieh
Sent: Sunday, December 12, 2021 1:32 AM
To: python-win32 at python.org
Subject: [python-win32] pywin can not access excel file

Hi,

We are using winpy32 to automate excel applications using python. The python script will be used by users with no administrator privileges, and in this case, we are getting the following error:
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', \"Microsoft Excel cannot access the file 'C:\\\\App\\\\Backend\\\\public\\\\MSB_2021\\\\11\\\\SYB_10_7_V1.xlsx'. There are several possible reasons: The file name or path does not exist. The file is being used by another program. The workbook you are trying to save has the same name as a currently open workbook.\", 'xlmain11.chm', 0, -2146827284)

Things to note:

  1.  The file does exist
  2.  The file is not being used by another program
  3.  The path is correct
  4.  The python script is being successfully used by users with administrative privileges

Please find bellow the method I am using to open the excel file:


import win32com.client as w3c



filename = 'C:\\\\App\\\\Backend\\\\public\\\\MSB_2021\\\\11\\\\SYB_10_7_V1.xlsx'

ExcelApp = w3c.DispatchEx("Excel.Application")
ExcelApp.Visible = False
ExcelApp.DisplayAlerts = False
wb = ExcelApp.Workbooks.Open("{0}".format(fileName))

Appreciate your help.

Best,
Tamara

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/python-win32/attachments/20211213/1b82b720/attachment.html>


More information about the python-win32 mailing list