python - Interaction between open and dispatched excel processes, win32com -


i using win32com (python 2.7 (anaconda) in liclipse) start separate instances of excel...

class exceldocument(object):     """excel class     """     def __init__(self, xldocin, make_visible=false):         """open spreadsheet"""         self.excelapp = dispatchex('excel.application') 

i bits , bobs excel document (using ms office 2013), including opening document dispatchex...

objexcel1 = exceldocument(path_table,false) objexcel1.update_sheets() ... objexcel2 = exceldocument(path_backg, false) 

trying assign second exceldocument class crash script.

if change init to

def __init__(self, xldocin, make_visible=false):         """open spreadsheet"""         try:             self.excelapp = getactiveobject('excel.application')         except:             self.excelapp = dispatchex('excel.application') 

the script runs fine.

if have excel file open when run script either a. script crash upon editing open file. b. open excel file close when these lines of code executed...

def close(self):         """close spreadsheet resources"""         self.excelapp.displayalerts = true         self.workbook.saved = 0         self.workbook.close(savechanges=0)         self.excelapp.visible = 0 

is there reason cannot call dispatchex second time, script should run in background , not interrupt open excel files?

you should design script assuming there can 1 instance of excel running, although can contain multiple workbooks. if use getobject (instead of getactiveobject) win32com return handle existing app if 1 exists, or start app if doesn't. won't need if/else.

also means in terms of design should have way of tracking workbooks opened, , close those, final state of excel application same when started script. have 1 instance of exceldocument per workbook, each using getobject, , each 1 closing workbook represents. before create first exceldocument, save getactiveobject script knows if should close app on exit.

basically:

activexlapp = win32com.client.getactiveobject('excel.application')   objexcel1 = exceldocument(path_table,false) # uses getobject() objexcel1.update_sheets() ... objexcel2 = exceldocument(path_backg, false)  if activexlapp not none:      activexlapp.close() 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -