Python win32com convert from xlsx/xls to xlsm? - python

I have seen many responses for win32com on how to convert from xlsm to xlsx or xls, but I am trying to do the exact opposite. I tried to mimic the code used to convert xlsm to xlsx but I get an error message. The code I'm using is:
import win32com.client as win32
from win32com import client
from shutil import copyfile
xlApp = client.gencache.EnsureDispatch('Excel.Application')
if file_original[-1] == "x" or file_original[-1] == "s": # If it is xlsx or xls
file_temp = copyfile(file_orig, str(main_folder) + "\\mainfile_duplicated_nomacro.xlsx")
books_temp = xlApp.Workbooks.Open(str(main_folder) + "\\mainfile_duplicated_nomacro.xlsx")
books_temp.SaveAs(Filename = "\\mainfile_duplicated.xlsm" , FileFormat = 52)
books_temp.Close(True)
elif anl_orig[-1] == "m": # If it is xlsm
file_dup = copyfile(file_orig, str(main_folder) + "\\mainfile_duplicated.xlsm")
When I save books_temp, I use FileFormat = 52, which is something I got from here. The error message I get is:
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel',
"Microsoft Excel cannot access the file 'C:\EDF0B000'. There are
several possible reasons:\n\n• The file name or path does not
exist.\n• The file is being used by another program.\n• The workbook
you are trying to save has the same name as a currently open
workbook.", 'xlmain11.chm', 0, -2146827284), None)
Where am I going wrong?

Related

Working with win32com in Python for opening .xslm files

I am using a function I got from another stack overflow article to open some Excel files in python, get all the charts on the first sheet, and save those as png files. I use it as part of a larger code I wrote to iterate through some excel files, change some select values, then get the changed charts.
import win32com.client
import PIL
from PIL import ImageGrab, Image
import os
import sys
def saveExcelGraphAsPNG(inputExcelFilePath, outputPNGImagePath,AC,mg,t):
# Open the excel application using win32com
o = win32com.client.Dispatch("Excel.Application")
# Disable alerts and visibility to the user
o.Visible = 0
o.DisplayAlerts = 0
# Open workbook
wb = o.Workbooks.Open(inputExcelFilePath)
# Extract first sheet
sheet = o.Sheets(1)
for n, shape in enumerate(sheet.Shapes):
# Save shape to clipboard, then save what is in the clipboard to the file
shape.Copy()
image = ImageGrab.grabclipboard()
# Saves the image into the existing png file (overwriting) TODO ***** Have try except?
outputPNGImage = outputPNGImagePath+str(AC)+'_' + str(n) +'_'+str(mg)+'_'+str(t)+ '.png'
image.save(outputPNGImage, 'png')
pass
pass
wb.Close(True)
o.Quit()
Particularly, the excel files i am iterating through are macro-enabled excel files (.xlsm). The function works fine for a few of the files, but then I eventually reach one of them and get the following error I do not get with the other xlsm files:
Traceback (most recent call last):
File "C:\Users\Desktop\Modeling\getGraphs.py", line 100, in <module>
saveExcelGraphAsPNG(preamb+ex_list[imp_nums.index(i)], preamb+outputPNGImagePath,i,mg[imp_nums.index(i)],t)
File "C:\Users\Desktop\Modeling\getGraphs.py", line 49, in saveExcelGraphAsPNG
wb = o.Workbooks.Open(inputExcelFilePath)
File "<COMObject <unknown>>", line 5, in Open
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'Open method of Workbooks class failed', 'xlmain11.chm', 0, -2146827284), None)
When opening the file after getting this error to check what could have happened, I get a warning that the file has been corrupted and I lose the 2 sheets (out of 4) that primarily employ the macros get deleted when I click to salvage what it can. This is baffeling me because before reaching this file the code works perfectly (and without corrupting) 3 other xlsm files. Any help or clues towards what might be the issue or a fix would be appreciated!
Thanks in advance!

Why am I getting 'Open method of Workbooks class failed' error when trying to open Excel file using pywin32?

So, I am using openpyxl and pandas to open an excel file, and write in data. Then, I'm attempting to use pywin32 to open the same file and run a macro to parse the data. But, I'm getting this error when attempting to open the file with pywin32:
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'Open method of Workbooks class failed', 'xlmain11.chm', 0, -2146827284), None)
This is the code I'm using with pywin32:
if os.path.exists(self.excel_parser_location):
# print "Opening Telematics_Messages_Parser.xlsm in Excel"
xl = client.Dispatch("Excel.Application")
xl.Application.visible = True
wb = xl.Workbooks.Open(os.path.abspath(self.excel_parser_location), ReadOnly=1)
And this is the code I'm using to write in the data before using pywin32:
if os.path.exists(csv_path):
data = pd.read_csv(csv_path, error_bad_lines=False)
book = openpyxl.load_workbook(self.excel_parser_location, keep_vba=True)
writer = pd.ExcelWriter(self.excel_parser_location)
writer.book = book
data.to_excel(writer, sheet_name='2 RawData', index=False)
# print 'Writing new data'
book.remove(book['2 RawData'])
# print 'Removing blank sheet'
book_sheet = book['2 RawData1']
book_sheet.title = '2 RawData'
# print 'Renaming sheet'
writer.save()
writer.close()
I've had a similar issue in the past that I resolved by using an older version of pywin32, but that isn't working now. I'm using pywin32 version 223.
Sometimes this error occured when path to file is too long. So you should try this code with file with shorter path

How can I write only the file name instead of writing the entire path?

I'm trying to use os.chdir so I can only write the name of the file within the directory(Table1.xlsx) instead of writing the entire path(r"C:\Users\crist\word_automation\Summary_template\Table1.xlsx"), but this code seems to not be working.
from win32com import client
import os
os.chdir(r"C:\Users\crist\word_automation\Summary_template")
excel = client.Dispatch("Excel.Application")
word = client.Dispatch("Word.Application")
doc = word.Documents.Open('Table1.docx')
book = excel.Workbooks.Open('Table1.xlsx')
sheet = book.Worksheets(1)
sheet.Range("A1:D5").Copy()
wdRange = doc.Content
wdRange.Collapse(0)
wdRange.PasteExcelTable(False, True, False)
os.remove('Table2.xlsx')
book.SaveAs('Table2.xlsx')
book.Close()
excel.Quit()
doc.SaveAs('TableOne.docx')
doc.Close()
word.Quit()
I get this error:
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "Sorry, we couldn't find Table1.xlsx. Is it possible it was moved, renamed or deleted?", 'xlmain11.chm', 0, -2146827284), None)
Try To Make Variable with the path like:
path = "desired/path/to/project"
and then whenever you want to interact with saving or loading files:
book.SaveAs(f'{path}/filename')

pywintypes.com_error opening Excel with Python

I have the following script which was immitated from here ( http://pythonexcels.com/python-excel-mini-cookbook/ ):
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open('words.xlsx')
and it returns the following error ( full traceback )
Traceback (most recent call last):
File "", line 1, in
wb = excel.Workbooks.Open('words.xlsx')
File "C:directory\Python35\lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x7\Workbooks.py", line 78, in Open
, Converter, AddToMru, Local, CorruptLoad)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "'words.xlsx' could not be found. Check the spelling of the file name, and verify that the file location is correct.\n\nIf you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.", 'xlmain11.chm', 0, -2146827284), None)
When I alternatively use openpyxl's functions to open the workbook there is no issue (referenced this https://automatetheboringstuff.com/chapter12/ ) . The python file and the excel file are in the same folder together. Am I calling something inappropriately?
I am certain that the file is spelled correctly ( words.xlsx ) and that it is in the same folder as the python file.
Any thoughts would be appreciated.
Try this:
import win32com.client as win32
import os
excel = win32.gencache.EnsureDispatch('Excel.Application')
path = os.getcwd().replace('\'','\\') + '\\'
wb = excel.Workbooks.Open(path+'words.xlsx')
Excepted a path error, not module or system error.
'xlmain11.chm' is empty, so don't need this.
Be careful when using escape characters on path-string.
Script and work file are in the same directory!
Hope that helps
Have you tried openpyxl, it's very easy to use, reading and writing excel files is no trouble
from openpyxl import Workbook
And initialize as
wb = Workbook()
ws = wb.active()
And you can start reading and writing right away

Run Excel from Python, Run Macro WITH Credentials

I have a block of code that I have used in the past to touch an excel file, run a macro already saved in the workbook, save and close. For example:
import win32com.client
import os
import os.path
file = 'myworkbook.xlsm'
path = 'C:/PATH/'
macro = 'mymacro'
filename = os.path.join(path, file)
xlApp = win32com.client.Dispatch("Excel.Application")
wkbk = xlApp.Workbooks.Open(filename)
runMacro = wkbk.Name + '!' + macro
xlApp.Run(runMacro)
wkbk.Save()
wkbk.Close(1)
xlApp.Quit()
xlApp = None
However, I would now like to use this on a macro that refreshes a data connection, and thus prompts for the password for the SQL database in question. When I try to run this, without worrying about the credentials, I get this error:
com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Office
Excel', u"Cannot run the macro 'MYMACRO'. The
macro may not be available in this workbook or all macros may be
disabled.", u'C:\Program Files (x86)\Microsoft
Office\Office12\1033\XLMAIN11.CHM', 0, -2146827284), None)
Is there a way to make this work---i.e. to pass the required credentials from python to excel so that the macro can run?

Categories