Python win32com 操作Exce的l簡單方法(必看)
更新時(shí)間:2017年05月25日 10:49:24 投稿:jingxian
下面小編就為大家?guī)硪黄狿ython win32com 操作Exce的l簡單方法(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
實(shí)例如下:
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): #關(guān)閉文件 self.xlBook.Close(SaveChanges=0) del self.xlApp def getCell(self, sheet, row, col): #獲取單元格的數(shù)據(jù) "Get value of one cell" sht = self.xlBook.Worksheets(sheet) return sht.Cells(row, col).Value def setCell(self, sheet, row, col, value): #設(shè)置單元格的數(shù)據(jù) "set value of one cell" sht = self.xlBook.Worksheets(sheet) sht.Cells(row, col).Value = value def setCellformat(self, sheet, row, col): #設(shè)置單元格的數(shù)據(jù) "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): #獲得一塊區(qū)域的數(shù)據(jù),返回為一個(gè)二維元組 "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): #復(fù)制工作表 "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簡單方法(必看)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python如何操作office實(shí)現(xiàn)自動(dòng)化及win32com.client的運(yùn)用
- 解決python中導(dǎo)入win32com.client出錯(cuò)的問題
- Python操作word常見方法示例【win32com與docx模塊】
- Python使用win32com模塊實(shí)現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)自動(dòng)生成word表格的方法
- Python使用win32com實(shí)現(xiàn)的模擬瀏覽器功能示例
- python使用win32com庫播放mp3文件的方法
- python字符串加密解密的三種方法分享(base64 win32com)
- Python 基于win32com客戶端實(shí)現(xiàn)Excel操作的詳細(xì)過程
相關(guān)文章
python通過對字典的排序,對json字段進(jìn)行排序的實(shí)例
今天小編就為大家分享一篇python通過對字典的排序,對json字段進(jìn)行排序的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02Python面向?qū)ο蟪绦蛟O(shè)計(jì)構(gòu)造函數(shù)和析構(gòu)函數(shù)用法分析
這篇文章主要介紹了Python面向?qū)ο蟪绦蛟O(shè)計(jì)構(gòu)造函數(shù)和析構(gòu)函數(shù)用法,結(jié)合具體實(shí)例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中構(gòu)造函數(shù)與析構(gòu)函數(shù)的概念、原理、功能及相關(guān)使用技巧,需要的朋友可以參考下2019-04-04python smtplib模塊實(shí)現(xiàn)發(fā)送郵件帶附件sendmail
這篇文章主要為大家詳細(xì)介紹了python smtplib模塊實(shí)現(xiàn)發(fā)送郵件帶附件sendmail,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05python回調(diào)函數(shù)用法實(shí)例分析
這篇文章主要介紹了python回調(diào)函數(shù)用法,較為詳細(xì)的分析了常用的調(diào)用方式,并實(shí)例介紹了Python回調(diào)函數(shù)的使用技巧,需要的朋友可以參考下2015-05-05淺談Python中重載isinstance繼承關(guān)系的問題
本篇文章主要介紹了淺談Python中重載isinstance繼承關(guān)系的問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05Python實(shí)現(xiàn)迷宮自動(dòng)尋路實(shí)例
大家好,本篇文章主要講的是Python實(shí)現(xiàn)迷宮自動(dòng)尋路實(shí)例,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02