Created
May 5, 2021 17:36
-
-
Save ProjectEli/d1bf189ca0cf5d862ce4978388edd89d to your computer and use it in GitHub Desktop.
Simple Microsoft Excel controller with win32com.client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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