Python win32com 操作Exce的l簡單方法(必看)
更新時間:2017年05月25日 10:49:24 投稿:jingxian
下面小編就為大家?guī)硪黄狿ython win32com 操作Exce的l簡單方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
實例如下:
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ù),返回為一個二維元組
"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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
PyTorch?TensorFlow機器學(xué)習(xí)框架選擇實戰(zhàn)
這篇文章主要為大家介紹了PyTorch?TensorFlow機器學(xué)習(xí)框架選擇實戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
python matplotlib庫繪制條形圖練習(xí)題
這篇文章主要介紹了python matplotlib庫繪制條形圖練習(xí)題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08
小議Python中自定義函數(shù)的可變參數(shù)的使用及注意點
Python函數(shù)的默認值參數(shù)只會在函數(shù)定義處被解析一次,以后再使用時這個默認值還是一樣,這在與可變參數(shù)共同使用時便會產(chǎn)生困惑,下面就來小議Python中自定義函數(shù)的可變參數(shù)的使用及注意點2016-06-06

