Skip to content

Instantly share code, notes, and snippets.

@ProjectEli
Created May 5, 2021 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ProjectEli/d1bf189ca0cf5d862ce4978388edd89d to your computer and use it in GitHub Desktop.
Save ProjectEli/d1bf189ca0cf5d862ce4978388edd89d to your computer and use it in GitHub Desktop.
Simple Microsoft Excel controller with win32com.client
import win32com.client, win32ui
class ExcelControl:
def __init__(self):
# Initialize Excel app
self.excel = win32com.client.Dispatch("Excel.Application")
self.excel.Visible=True
def Openfile(self):
# open file
self.dlg = win32ui.CreateFileDialog(1) # 1 : Open File dialog box
self.dlg.DoModal()
self.filename = self.dlg.GetPathName()
print(self.filename)
if self.filename != None:
self.wb = self.excel.Workbooks.Open(self.filename)
self.ws = self.wb.ActiveSheet # Not necessarily active sheet
else:
self.wb = self.excel.Workbooks.Add()
self.ws = self.wb.Worksheets("Sheet1")
def SetCellValue(self,row_index, col_index,value):
# set value at specific cell position
self.ws.Cells(row_index,col_index).Value = value
def UndoLast(self):
pass # to be implemented
# self.excel.Undo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment