国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python win32com 操作Exce的l簡單方法(必看)

2020-01-04 17:14:10
字體:
來源:轉載
供稿:網友

實例如下:

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()

以上這篇Python win32com 操作Exce的l簡單方法(必看)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浮山县| 延安市| 昭苏县| 开封县| 拉萨市| 海宁市| 留坝县| 邢台市| 巫山县| 张掖市| 芮城县| 曲阜市| 鄂伦春自治旗| 重庆市| 宜州市| 孟州市| 东丰县| 普格县| 青州市| 昌吉市| 乐业县| 北宁市| 梧州市| 宝山区| 崇信县| 沙湾县| 通化市| 颍上县| 祁阳县| 沙雅县| 满城县| 大名县| 博乐市| 昌都县| 德保县| 林甸县| 兴文县| 德清县| 黄梅县| 兴隆县| 高平市|