Run Excel VBA code from newly created Excel file using Python - python

I am trying to run Excel macros from ABAQUS software tool which has inbuilt Python.
I have a default Excel file, I want this as a main Excel file and to make a new Excel file depending on the output database file.
I access the ABAQUS output data base file and get its name and make a new Excel file named by combining the output data base file name plus some other name.
I copy the main Excel file to this new Excel file.
I am able to open this newly created file, but I am unable to run the macros.
Here is the code with comments.
I get the path of my file with name and extension
filePath = odbFile.path
Filename with extension only
filename_w_ext = os.path.basename(filePath)
odbfilename,odbfileExtension = os.path.splitext(filename_w_ext)
excel_filename='abc.xlsm'
odbfilename+'_FAT'+'.xlsm'
newExcelFile = odbfilename+'_FAT'+'.xlsm'
shutil.copy(excel_filename, newExcelFile)
I access the win32client to access the Excel application
xl = win32com.client.Dispatch("Excel.Application")
Opening the Excel file, give the file path
xl.Workbooks.Open(newExcelFile)
Say newExcelFile created name is new.xlsm.
To run the Excel macros using PYTHON, the default code is
xl.Application.Run("excelName!ModuleNumberOfTheMacro.MacroName")
Run my macro to import nodes from Excel
xl.Application.Run("newExcelFile!Module1.ImporterFichiersTextes")
I put down the same format but Python is looking for the Excel sheet 'newExcelFile', instead of new.xlsm.

Related

Excel file can't be opened after saved from Tkinter

I am using xlsxwriter library to write data in excel file and it works fine, when excel file is saved in root directory.
I want to enable user to choose where to save the file, so I am using from asksaveasfile from tkinter.filedialog :
files = [('Excel Document', '.xlsx'), ('All Files', '*.*')]
workbook = asksaveasfile(filetypes=files, defaultextension=files)
The file saves in specified folder, but won't open:
Excel cannot open the file
I also tried .xls , again, file can't be opened.
Thank you in advance!
Your code is not complete and can not be fixed if you do not include the saving part (what happens to workbook after user choose file to save?).
It seems you are writing an empty file. Check if it has 0 size or not?

how to upload .csv file to azure folder in python

Anyone know to to upload a .csv file to a folder inside a blob container in python?
i'm having difficulty trying to acess the folders inside it.
i have the csv and want to save it inside the blob folder, but it didn't work.
the file is in code, so i dont want to pass the directory where it is.
csv = df.to_csv()
block_blob_service.create_blob_from_path(container_name, 'folder/csv/mycsv/' , csv)
Someone knows how i can save the csv directly to the folder inside the storage folder (folder/csv/mycsv/) in azure?
i got an error stat: path too long for Windows
Reading the documentation of DataFrame.to_csv, I believe csv variable actually contains string type data. If that's the case, then you will need to use create_blob_from_text method.
So your code would be:
csv = df.to_csv()
block_blob_service.create_blob_from_text(container_name, 'folder/csv/mycsv/' , csv)

VBA script to trigger Python and saving down xlsx file

I am trying to save the .xlsx file and using VBA as a wrapper to execute the python file. However, I can validate that python code runs, but somehow .xlsx file is not saved. When I run the same python file via IDE, it saves down the .xlsx file.
VBA code
Sub RunPython()
Dim shell As Object
Dim exepath, scriptPath As String
Set Shell = VBA.CreateObject("Wscript.Shell")
exePath = """C:\Program Files\...\python.exe"""
scriptPath = "C:\....\mymodule.py"
Python Script
import pandas as pd
import xlsxwriter
df = pd.DataFrame(np.random.randn(5,2),index=range(0,10,2),columns =list('AB'))
excel_file ='sample.xlsx'
writer = pd.ExcelWriter(excel_file,engine='xlsxwriter')
df.to_excel(writer,sheet_name='Data')
print ("file read")
writer.save()
writer.close()
Does that 'excel_file' have a path?
Does Python use a current directory and automatically add it to Excel file name?
If yes, do you know how to obtain it and can you look for .xlsx file there?
If not, how do you check if the .xlsx file has been saved? Where are you looking for?
Does Python raise any error?
I am looking to the code mostly like a VBA user...
I would suggest to send a message from Python, containing the current directory path. Supposing that the .xlsx workbook path has not somehow been defined and the code presented here does not include that part. Theoretically it should use the same current directory, but I think it is good to be checked...

How do I upload an .xlsx file to an FTP without creating a local file?

I am writing a script to pull xml files from an FTP, turn them into an .xlsx file, and re-upload to a different directory on the same FTP. I want to create the .xlsx file within my script instead of copying the xml data into a template and uploading my local file.
I tried creating a filename for the .xlsx doc, but i realize that i need to save it before i can upload to the FTP. My question is, would it be better to create a temporary folder on the server the script is being run and empty the folder out afterwards? or is there a way to upload the doc without saving it anywhere (preferred)? I will be running the script on a windows server
ftps.cwd(ftpExcelDir)
wbFilename = str(orderID + '.xlsx')
savedFile = saving the file somwhere # this is the part im having trouble with
ftps.storline('STOR ' + wbFilename, savedFile)
With the following code, i can get the .xlsx files to save to the FTP, but i recieve an invalid extension/corrupt file error from Excel:
ftps.cwd(ftpExcelDir)
wbFilename = str(orderID + '.xlsx')
inMemoryWB = io.BytesIO()
wb.save(inMemoryWB)
ftps.storbinary('STOR ' + wbFilename, inMemoryWB)
The ftp functions take file objects... but those don't strictly speaking need to be files. Python has BytesIO and StringIO objects which act like files, but are backed by memory. See: https://stackoverflow.com/a/44672691/8833934

How to save Xlsxwriter file in certain path?

Where does Xlsxwriter save the files you have created? Is it possibly to specify the path where I want the excel files to be saved?
My XlsxWriter script was in file /app/smth1/smth2/ and for some reason it saved the excel file to /app/. Shouldn't it have saved it in the same file where the script was? Or do I have to specify the path like this:
workbook = xlsxwriter.Workbook(' /app/smth1/smth2/Expenses01.xlsx')
What is the default file where the excel file is saved?
Here's how you can save the file to the current directory (where your script is running from):
workbook = xlsxwriter.Workbook('demo.xlsx')
Here's how you can specify a full path:
workbook = xlsxwriter.Workbook('C:/Users/Steven/Documents/demo.xlsx')
Here's how you can specify a relative path:
workbook = xlsxwriter.Workbook('app/smth1/smth2/Expenses01.xlsx')
Note that a starting "/" is not needed and may cause errors.
More examples can be found here

Categories

Resources