python operation macro having excel

First, the use win32com library

Install install pypiwincom PIP 

Import win32com.client
 # Excel 
xlApp = win32com.client.DispatchEx ( " Excel.Application " ) 

# running in the background, no display, no warning 
xlApp.Visible = 0 
xlApp.DisplayAlerts = 0 
FileName = r " C: \ the Users \ ffm11 \ Desktop \ mydata.xls " 
# Excel 
xlBook = xlApp.Workbooks.Open (FileName)
 # shield pop 
xlBook.Checkcompatibility = False
 the try :
     # Get Sheet 
    _sheet = xlBook.Worksheets ( ' Sheet1 ') 
    _Sheet2 = xlBook.Worksheets ( ' Sheet2 ' )
     # acquiring specified cell 
    Print (_sheet.Cells (1,1 ) .Value) 

    # Printer 
    # _sheet.PrintOut () 

    # cycle accessories cell cell 
    datatupe = _sheet.UsedRange. the Value
     Print (datatupe) 

    # all cells is set to null 
    _sheet.UsedRange.Value = "" 

    # assignment sheet content 
    _sheet.UsedRange.Value = _sheet2.UsedRange.Value
     # Excel 
    # xlBook.SaveAs (FileName) # Save as 
    xlBook. the Save ()
 the except Exception AS E:
    print(e)

finally:
    #excel
    xlBook.Close()
    xlApp.Quit()

Guess you like

Origin www.cnblogs.com/angelyan/p/12041625.html