Python win32com 에서 exce 를 조작 하 는 l 간단 한 방법(필수)

4179 단어 win32comexcelPython
실례 는 다음 과 같다.

from win32com.client import Dispatch  
import win32com.client  
class easyExcel:  
   """A utility to make it easier to get at Excel.  Remembering 
   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 save(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 setCell(self, sheet, row, col, value): #         
     "set value of one cell"  
     sht = self.xlBook.Worksheets(sheet)  
     sht.Cells(row, col).Value = value 
   def setCellformat(self, sheet, row, col): #         
     "set value of one cell"  
     sht = self.xlBook.Worksheets(sheet)  
     sht.Cells(row, col).Font.Size = 15#     
     sht.Cells(row, col).Font.Bold = True#     
     sht.Cells(row, col).Name = "Arial"#     
     sht.Cells(row, col).Interior.ColorIndex = 3#     
     #sht.Range("A1").Borders.LineStyle = xlDouble 
     sht.Cells(row, col).BorderAround(1,4)#     
     sht.Rows(3).RowHeight = 30#   
     sht.Cells(row, col).HorizontalAlignment = -4131 #    xlCenter 
     sht.Cells(row, col).VerticalAlignment = -4160 # 
   def deleteRow(self, sheet, row): 
     sht = self.xlBook.Worksheets(sheet) 
     sht.Rows(row).Delete()#    
     sht.Columns(row).Delete()#   
   def getRange(self, sheet, row1, col1, row2, col2): #         ,          
     "return a 2d array (i.e. tuple of tuples)"  
     sht = self.xlBook.Worksheets(sheet) 
     return sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Value  
   def addPicture(self, sheet, pictureName, Left, Top, Width, Height): #     
     "Insert a picture in sheet"  
     sht = self.xlBook.Worksheets(sheet)  
     sht.Shapes.AddPicture(pictureName, 1, 1, Left, Top, Width, Height)  
   
   def cpSheet(self, before): #      
     "copy sheet"  
     shts = self.xlBook.Worksheets  
     shts(1).Copy(None,shts(1))

   def inserRow(self,sheet,row):
     sht = self.xlBook.Worksheets(sheet)
     sht.Rows(row).Insert(1)

   #         。
if __name__ == "__main__":  
   #PNFILE = r'c:/screenshot.bmp' 
   xls = easyExcel(r'd:\jason.li\Desktop\empty_book.xlsx')   
   #xls.addPicture('Sheet1', PNFILE, 20,20,1000,1000)  
   #xls.cpSheet('Sheet1') 
   xls.setCell('sheet1',2,'A',88) 
   row=1 
   col=1 
   print("*******beginsetCellformat********") 
   # while(row<5):
   #  while(col<5):
   #    xls.setCellformat('sheet1',row,col)
   #    col += 1
   #    print("row=%s,col=%s" %(row,col))
   #  row += 1
   #  col=1
   #  print("*******row********")
   # print("*******endsetCellformat********")
   # print("*******deleteRow********")
   # xls.deleteRow('sheet1',5)
   xls.inserRow('sheet1',7)
   xls.save()  
   xls.close()
이 파 이 썬 win32com 에서 Exce 를 조작 하 는 l 간단 한 방법(필수)은 바로 편집장 이 여러분 에 게 공유 한 모든 내용 입 니 다.참고 하 실 수 있 고 많은 응원 부 탁 드 리 겠 습 니 다.

좋은 웹페이지 즐겨찾기