2

Hi I am new to Python scripting, I wrote a code for SAP GUI automation but at last excel file open so I want to close that file.

I need a help regarding code to check if specific Excel file is already open, if open then closing the same using Python.

Thanks for help in advance.

I am using below code:

        with open('C:\\Users\\Xyz\\AppData\\Local\\Temp\\export.XLSX', 'r') as f:
        f.close()

Also if someone can hep me converting VBA function in Python and can tell me how to call the same.

Function WKopen(wb As String) As Boolean
On Error Resume Next
WKopen= Len(Workbooks(wb).Name)
End Function

Sub BK_Close()
On Error Resume Next
If WkOpen("Export_Download.XLSX") Then
Workbooks("Export_Download.xlsx").Close savechanges:=False
Else
End If
End Sub
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Suhan Dev
  • 23
  • 1
  • 1
  • 4

2 Answers2

0
import xlwings as xw
try:
    book = xw.Book('export.xlsx')
    book.close()
except Exception as e:
    print(e)

This will close the workbook if it is open. You may need to let python wait for SAP to open the file so something like the following may be necessary prior to trying to close the workbook

import time
#wait for SAP to open file
time.sleep(3)

I can't help with the second part of your question.

-1

Ok. You have actually used two methods of closing a file together.

You should either use with which closes the file as soon as you come out of the with block.

Or

You can open your file without with like:

f = open('C:\\Users\\Xyz\\AppData\\Local\\Temp\\export.XLSX', 'r')

and then close using f.close().


Now, since, you are new, the best way to open Excel files on Python is to use pandas library's read_excel function.

So, the code will be:

import pandas as pd
pd.read_excel('C:\\Users\\Xyz\\AppData\\Local\\Temp\\export.XLSX')

Here's the documentation for it, if you need something more from this function, here.

Abhishek Verma
  • 1,671
  • 1
  • 8
  • 12
  • No, my file is already open so I want to check if the file is open or not and if open then want to close it – Suhan Dev May 15 '20 at 08:55
  • Use f.closed, if it is True then the file closed if not then the file is open. – Abhishek Verma May 15 '20 at 19:28
  • Thanks Abhishek but I tried everything its not working. my excel file get open after execution of script so now I want to write a code to check..... condition 1 - check if my file is open. condition 2 - If yes then just close it. condition 3 - If no then do nothing. – Suhan Dev May 16 '20 at 06:32
  • Yes, I have created a script to download data from SAP but one the file get saved in particular location it get open automatically so now I have challenge of closing the same file in python. – Suhan Dev May 16 '20 at 14:09
  • Hi, I am having an excel sheet named temp.xlsx. I am using temp.to_excel() to write to this excel sheet followed by read_excel() to read from the same excel file. This process is happening in a loop of 50 iterations. At one point in time, I am getting an error that "the file is already open and it says PermissionError: [Errno 13] Permission denied:" Why it is happening? Any ideas? – ankush jamthikar Feb 06 '21 at 07:52
  • @SuhanDev How did you manage to close it? I am still stuck at this point. f.closed returns true. But f.close() is not helping me. It is not closing the file – Priya May 26 '21 at 08:42
  • @AbhishekVerma f.closed returns true. But f.close() is not helping me. It is not closing the file – Priya May 26 '21 at 08:42
  • @Priya f.closed means the file is closed. How are verifying that file is not closed? – Abhishek Verma May 27 '21 at 18:42