I am currently attempting to write several pandas dataframes to an existing excel workbook. I chose xlwings because it was successful with preserving the formulas in the workbook. This code worked for about two days, and then an ambiguous error surfaced. Any idea why this code fails?
def write_packaged_outputs(packaged_outputs):
print("Writing to template")
template_file = './SlotVizTemplate.xlsx'
output_file = './SlotVizOutput.xlsx'
workbook = xlwings.Book(template_file)
for packaged_output in packaged_outputs:
df = packaged_output['dataframe']
sheet_name = packaged_output['sheet_name']
cell = packaged_output['cell']
worksheet = workbook.sheets[sheet_name]
worksheet.range(cell).options(index=False, header=False).value = df
workbook.save(output_file)
xlwings.apps[0].quit()
The code seems to fail at the workbook assignment:
workbook = xlwings.Book(template_file)
Here is the error:
Traceback (most recent call last):
File "C:\Users\philector\Documents\Development\Python\SlotViz\SlotVizTool_v1.py", line 214, in <module>
write_packaged_outputs(packaged_outputs)
File "C:\Users\philector\Documents\Development\Python\SlotViz\SlotVizTool_v1.py", line 160, in write_packaged_outputs
workbook = xlwings.Book(template_file)
File "C:\Users\philector\AppData\Local\Programs\Python\Python36-32\lib\site-packages\xlwings\main.py", line 479, in __init__
app = App(add_book=False)
File "C:\Users\philector\AppData\Local\Programs\Python\Python36-32\lib\site-packages\xlwings\main.py", line 204, in __init__
self.impl = xlplatform.App(spec=spec, add_book=add_book)
File "C:\Users\philector\AppData\Local\Programs\Python\Python36-32\lib\site-packages\xlwings\_xlwindows.py", line 288, in __init__
self._xl = COMRetryObjectWrapper(DispatchEx('Excel.Application'))
File "C:\Users\philector\AppData\Local\Programs\Python\Python36-32\lib\site-packages\win32com\client\__init__.py", line 113, in DispatchEx
dispatch = pythoncom.CoCreateInstanceEx(clsid, None, clsctx, serverInfo, (pythoncom.IID_IDispatch,))[0]
pywintypes.com_error: (-2147024703, 'OLE error 0x800700c1', None, None)
enter code here
I tried to figure out what the error code was, and came up with this:
I am just unsure what the error means, or what I need to do to fix it.
UPDATE
Launching an Excel instance with pypiwin32 module fails also, so I thought I would reinstall to if that fixes it. Does not. We get a similar error message as we did with xlwings.
import win32com.client
win32com.client.Dispatch('Excel.Application')
UPDATE 2
I think I found the problem. I have a 64bit machine, and for some reason the company I work for installed a 32bit version of Office 2016.
User contributions licensed under CC BY-SA 3.0