Python Openpyxl return a sheet name which doesn't exist - openpyxl

In my excel file, I have only three worksheet: "Slot 14", "Data Display", and "Ctrl Value".
When I use openpyxl to load excel file, it returns other worksheet which doesn't exist: ['Slot 14', 'DETAILNO-14', 'DETAIL-14', 'PNO-14', 'DATA-14', 'Data Display', 'Ctrl Value']
Following is my code
filepath=r'D:\Users\chshiu\Desktop\filename.xlsx'
wb = openpyxl.load_workbook(filepath)
wb.get_sheet_names()
I don't think there is something wrong in my code. I am wondering maybe the problem is from excel file itself? I have VBA code inside the excel file.
More information:
I use Python3 in windows.
Because openpyxl cannot load xls file now so I save my original xls file into xlsx file.

It's possible that your file contains hidden sheets. Older versions of Excel use things called Macrosheets for some of the GUI controls. openpyxl just reports what it finds.

Related

How to use macro enabled excel file in HP UFT?

I am using below code
import Datatable.ExportSheet path, sourcefile, destinationfile
And I am getting below error
The DataTable.ImportSheet operation failed. invalid file error
Assuming that you are getting an error message while importing a macro enabled Excel into UFT datatable
Answer is, you can't import a macro enabled Excel (xlsm) file into UFT datatable. The reason being, Datatable.ImportSheet accepts only .xls or .xlsx file extensions.
Workaround is, save the xlsm file as Excel 97-2003 Workbook (xls) format using Save As function from Excel. Or save the xlsm file as Excel Workbook (xlsx) format.

Excel VBA: SaveCopyAs with different file extenstion

I have an Excel file with .xlsb extenstion and use its macros to generate several other Excel sheets based on the contents. The macros work in a way that they change the original Excel file and then use the SaveCopyAs method to save the generated Excel sheets.
The generated Excel sheets should be saved with .xlsx extension and format.
Using the ActiveWorkbook.SaveCopyAs "C:\TEMP\XXXX.XLSX" method is not working for me because while it does change the extension it does NOT change the file format so when a user opens a generated Excel file he receives a warning message (something like "the file extension and format does not match"). The SaveCopyAs method does not have any other arguments.
How can I save the copies of my original .xlsb file with both the extension and format to be changed to .xlsx?
Note: the Workbook.SaveAs method does have a fileformat option, not sure if that helps / relevant.
Based on the hint by Zac, in my case its a better solution to copy the tab with the relevant changes into a freshly created excel file and then save it with the new filename.
ThisWorkbook.Sheets("myTab").Copy
ActiveWorkbook.SaveAs Filename:="c:\temp\xyz.xlsx", FileFormat:=51
ActiveWorkbook.Close
This is actually a lot better solution for me as the end user really need the generated tab only and not the macros or any other data in the orginal excel file.

python open the latest excel file

I recently started using python 3.6 (trough Anaconda) so I am sorry if the below will result not that clear. The situation is the following:
I have an excel file, which I use to run a macro in order to obtain results from Prophet. My macro works good but, unfortunately, after several times it has been used excel reaches the limit of memory and I cannot extract the results anymore. The way to solve the problem would be to close excel and then open it again.
To do so, in the same vba macro, I open a python script that allows me to run the macro, save the excel file, close it and open it again. The macro is the below:
import win32com.client
object = win32com.client.Dispatch('Excel.Application')
object.visible=1
object.Run("Central_Run")
print("Macro ran successfully!")
print("Active WB:", object.ActiveWorkbook.Name)
for wb in object.Workbooks:
print("WB:",wb.Name)
wb.Save()
#The loop above saves all the workbooks that are open
object.quit()
#Open the Excel file again
object=win32com.client.Dispatch('Excel.Application')
wb=object.Workbooks.open(‘myfile.xlsm')
The script works, however, I would like to open the excel file, in the second part of the script, without specifying its name (wb=object.Workbooks.open(‘myfile.xlsm')), so that I can use the code for other similar excel files with different name. Ideas that I had were either to open the latest used excel file or to link the script to a specific cell in my excel file so that I need just to modify that cell instead of the script, but, unfortunately, I am not able to do it.
Thanks in advance for the help! :)
You can replace your second print statement with this:
wbName=object.ActiveWorkbook.Name
print("Active WB:", wbName)
and then replace the last line with:
wb=object.Workbooks.open(wbName)

Save workbook along with vba code

I've created a workbook which contains some macros along with it. My task is to save it including macros so that I'll be able to run them on the saved workbook. When I try to save it in ".xlsm" format--which is a standard format to save vba code--it's getting saved however, I'm unable to open the workbook from the desired saved file. The following window is displayed
" Excel cannot open the file "file.xlsm" because the file format or extension is not valid. Verify that the file is not corrupted and that the file extension matches the format of the file"
Can someone help me with this ? I've tried using ".xls",".xltm" formats as well. But, they don't save the vba code.
Try manually changing the file extension to ".xls" and then open it. If it opens, then do a "save as" in Excel and save it as "macro-enabled" (.xlsm).

change worksheet name, a difficult one

When I use excel to open a .txt file (a notepad file), the worksheet name is the file name of the notepad file that was opened by default. Therefore, the sheet name will be different when open a different notepad file. Downstream code need this worksheet name be a fixed one. Is there anyway to make change the sheet name to a fixed name such as "sheet1". By the way, codename can not be used, since the macro to use the data in the open file is not another workbook.
Thanks!
You don't need the codename not the worksheet name when you are opening .txt files from Excel. There will always be 1 sheet. So in your code you can always address that sheet as
wb.Sheets(1)
Where wb is the workbook object.
For your reference every .txt file that you open with VBA cannot have a common name unless you set it via code. And if you do that, you will have to still use wb.Sheets(1)
For example
wb.Sheets(1).Name = "Blah Blah"
Could you call your text file sheet1.txt? Would that solve your problem?
I am picturing that your macro opens the text file dynamically because you want to use excel's built in csv parsing. Perhaps sorting and filtering the data afterwards.
Siddarth gave you a good lead, but you shouldn't worry about the name or the sheet because as he said you have the worksheet object to use for your downstream code.
wb.Sheets(1)
Now, if you want to reference this sheet outside of the subroutine that you opened the file. Use a global variable for your
wb