Python uses win32com to manipulate excel

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.