Example 1
Import Win32com.client as Win32
XL = win32. Dispatch (' Excel.Application ')
Xl. Visible = True
Xl. Workbooks.Add ()
Xlbook = XL. Workbooks (1)
Xlsheet = XL. Sheets (1)
Xlsheet.cells (a). Value = ' What shall is the number of thy counting? '
Xlsheet.cells (2,1). Value = 3
Print Xlsheet.cells (a). Value
Print Xlsheet.cells (2,1). Value
Example 2
#coding: Utf-8
Import Win32com.client as Win32
Import time
Import pythoncom
now = Time.time ()
Print now
Time_object = pythoncom. Maketime (now)
print int (time_object)
XL = win32. Dispatch (' Excel.Application ')
Xl. Visible = True
Xl. Workbooks.Add ()
Xlbook = XL. Workbooks (1)
Xlsheet = XL. Sheets (1)
Xlsheet.cells (a). Value = ' What shall is the number of thy counting? '
Xlsheet.cells (2,1). Value = 3
#print xlsheet.cells (2,1). Value
Xlsheet.cells (3,1). Value = Time_object
#print xlsheet.cells (3,1). Value
Xlsheet.cells (4,1). Formula = ' =a2*2 '
#print xlsheet.cells (4,1). Value
#print xlsheet.cells (4,1). Formula
Xlsheet.cells (a). Value = None
#print Xlsheet.cells (a). Value
MyRange1 = Xlsheet.cells (4,1) #一个单元格
MyRange2 = Xlsheet.range ("B5:C10") #
MyRange3 = Xlsheet.range (Xlsheet.cells (2,2), Xlsheet.cells (3,8))
Example 3
Class Easyexcel:
"" A utility to make it easier to get at Excel. Remebering to
Save the data is your problem, as is error handling.
Operates on one workbook at a time. "" "
def __init__ (self, Filename=none):
Self.xlapp = Win32com.client.dispatch (' Excel.Application ')
If filename:
Self.filename = filename
Self.xlbook = self.xlApp.Workbooks.Open (filename)
Else
Self.xlbook = SELF.XLAPP.WORKBOOKS.ADD ()
Self.filename = ""
def sace (self, Newfilename=none):
If NewFileName:
Self.filename = NewFileName
Self.xlBook.SaveAs (NewFileName)
Else
Self.xlBook.Save ()
def close (self):
Self.xlBook.Close (savechanges=0)
Del Self.xlapp
def getcell (self, sheet, row, col):
"Get value of one cell"
Sht = self.xlBook.Worksheets (sheet)
Return sht. Cells (Row, col). Value
def set (self, sheet, row, col, value):
"Set value of one cell"
Sht = self.xlBook.Worksheets (sheet)
Sht. Cells (Row, col). Value = value
Python uses win32com to manipulate excel