-1

I have the following error in my code, I have tried everything and I can not solve:

1° Not Successful

excel = win32com.client.Dispatch("Excel.Application")
wb = excel.Workbooks.Open(os.path.join(os.getcwd(),diretorio_BKP + filename +
                          '  '+str(nome_arq_parcial)+' -.xlsx'))
ws = wb.Worksheets(1).Select()
ws = wb.Worksheets(1)
print('abrir o arquivo de novo')

2° Not Successful

excel = win32com.client.Dispatch("Excel.Application")
wb = excel.Workbooks.Open(diretorio_BKP + filename +'  '+
                          str(nome_arq_parcial)+' -.xlsx')
ws = wb.Worksheets(1).Select()
ws = wb.Worksheets(1)
print('abrir o arquivo de novo')

The two attempts return the following problem to me:

ERRO:    (-2147352567, 'Exception.', (0, 'Microsoft Excel', "Microsoft Excel does not you can access the file '\\\\ IEMEN \\ file location -.xlsx'. There are several reasons possible: \ r \ n \ r \ n • The file name or path does not exist. \ r \ n • The file is being used by another program. \ r \ n • The workbook you are trying to save has the same name as another currently open \ r \ nfolder. ", 'xlmain11.chm', 0, -2146827284), None)

As you can see already tried to leave in absolute path, already tried to leave the full path, I already put excel.Application.Quit() before that stretch as well and nothing.

I need the code to go through this step to start formatting the whole file.

Community
  • 1
  • 1
  • 2
    Stack Overflow em Português -> https://pt.stackoverflow.com/ – Alex K. May 28 '19 at 16:45
  • Por favor, faça uma pergunta em inglês ou [edit](https://stackoverflow.com/posts/56346765/edit) para traduzir. Obrigado pela compreensão. – user3837868 May 28 '19 at 16:54
  • Please either completely translate the question (title, error messages), or post on the appropriate language site. – Blorgbeard May 28 '19 at 17:05
  • Possible duplicate of [excel access denied with win32 python pywin32](https://stackoverflow.com/questions/17177612/excel-access-denied-with-win32-python-pywin32) – Purin May 31 '19 at 14:03

2 Answers2

0

As the error messages details, there are several possible reasons for this error:

  1. "The file name or path does not exist" - This means that the file you are trying to open was never created OR it may be that the path to the file is incorrect.
  2. "The file is being used by another program" - This means that another program is actively using this file. You cannot make changes to an excel file while it is in use.
  3. "The workbook you are trying to save has the same name as another currently open" - This means that the file name of the excel file that you are trying to work with is already taken inside that directory and that the other file is open or in use by another program.

Check to make sure that you are not violating any of the items above.

Purin
  • 56
  • 9
0

SOLUTION! AMAZINGLY:

I could not believe how this error was obscure and we found the solution looking for similar problems with the equivalent .NET code:

To fix it, create a folder named 'Desktop' under 'C: \ Windows \ SysWOW64 \ config \ systemprofile \' on 64-bit architecture or 'C: \ Windows \ System32 \ config \ systemprofile \' on 32-bit servers.

WORKED WITH PERFECTION.

(Source: StackOverflow answers in English: excel access denied with win32 python pywin32)