日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python操作Excel——win32com模块和xlrd+xlwt+xlutils组合

發(fā)布時間:2023/12/9 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python操作Excel——win32com模块和xlrd+xlwt+xlutils组合 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

今天,接到一個任務(wù),要生成大約兩百個excel文件,從2006年到2013年,每個月兩個文件,這些文件中除了幾個關(guān)于日期的單元格不同外,其他數(shù)據(jù)都相同,所以就想到可以用python寫一個小腳本,自動生成文件。

從網(wǎng)上查找到python中操作Excel文件主要有兩個模塊,分別為win32com模塊和xlrd+xlwt+xlutils組合

win32com模塊很強(qiáng)大,但是讀取文件的速度稍慢,而且只能在ms系統(tǒng)運(yùn)行。

xlrd+xlwt+xlutils組合,xlrd只能讀取excel文件,xlwt只能修改文件,xlutils可以復(fù)制excel文件,但要借助xlrd和xlwt才能運(yùn)行,所以一般都是這三個模塊結(jié)合起來使用(感覺好麻煩,要下載三個模塊)。這個組合讀取文件的速度很快,不過對于樣式的把控不太好,復(fù)制的文件與原文件的樣式有點(diǎn)區(qū)別,如果對樣式的要求不高可以使用。

由于xlrd+xlwt+xlutils組合對樣式的把控不會,所以最后我是用win32com模塊成功實(shí)現(xiàn)腳本的。如果是ms系統(tǒng),也推薦大家用win32com模塊。

win32com的輔助類easyExcel:

#!/usr/bin/env python # -*- coding: utf-8 -*- from win32com.client import Dispatch import win32com.client class easyExcel:"""A utility to make it easier to get at Excel. Rememberingto 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 = filenameself.xlBook = self.xlApp.Workbooks.Open(filename)else:self.xlBook = self.xlApp.Workbooks.Add()self.filename = '' def save(self, newfilename=None):if newfilename:self.filename = newfilenameself.xlBook.SaveAs(newfilename)else:self.xlBook.Save() def close(self):self.xlBook.Close(SaveChanges=0)del self.xlAppdef getCell(self, sheet, row, col):"Get value of one cell"sht = self.xlBook.Worksheets(sheet)return sht.Cells(row, col).Valuedef setCell(self, sheet, row, col, value):"set value of one cell"sht = self.xlBook.Worksheets(sheet)sht.Cells(row, col).Value = valuedef 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)).Valuedef 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.Worksheetsshts(1).Copy(None,shts(1))

類的使用例子

myExcel=easyExcel("E:\python\sb.xls") #實(shí)例化一個easyExcel類
myExcel.setCell(
1,12,6,"hello") #修改單元格內(nèi)容,第一個參數(shù)是sheet的編號,第二個為行數(shù),第三個為列數(shù),(全部都以1開始,下面的xlrd那幾個模塊都以0開始的),最后是要修改的內(nèi)容
a=myExcel.getCell(1,12,6) #獲取單元格內(nèi)容

myExcel.save(
"E:\python\sb1winD.xls") #保存文件,如果路徑與打開時相同,即保存文件,如果不同即新建文件 myExcel.close()

#其他api可以參考easyExcel類

?

我的腳本的實(shí)現(xiàn)代碼:

#encoding=utf-8import easyExcel from time import sleep myExcel=easyExcel.easyExcel("E:\python\sb2.xls")def getLastDay(year,month):if month in [1,3,5,7,8,10,12]:return "31"elif month==2:if year in ["2008","2012"]:return "29"else: return "28"else:return "30"for i in range(2006,2014):for j in range(1,13): #if 1: # i=2006 # j=2getDateText="所得期間: %s年 %s月"%(i,j)lastDay=getLastDay(i,j)writeDateText="填表日期: %s年 %s月 %s日 "%(i,j,lastDay)getDateText=getDateText.decode("utf-8")writeDateText=writeDateText.decode("utf-8")dateStar="%s-%s-1"%(i,j)dateEnd="%s-%s-%s"%(i,j,lastDay)myExcel.setCell(1,4,1,writeDateText)myExcel.setCell(1,4,12,getDateText)myExcel.setCell(1,12,6,dateStar)myExcel.setCell(1,12,7,dateEnd)myExcel.save("E:\python\sb2\SB009-2--%s-%s.xls"%(i,j))print "Save ",i,j#wFile.save("sb1%s-%s.xls"%(i,j)) myExcel.close() print "DONE"

?

?xlrd+xlwt+xlutils 教程

xlrd

1.打開文件(不能用中文文件名,只能打開xls文件)

r_file =xlrd.open_workbook("demo1.xls")

2.獲取工作表

sheet0=r_file.sheets()[0]#通過索引順序獲取

sheet0 = r_file.sheet_by_index(0) #通過索引順序獲取

sheet0 = r_file.sheet_by_name(u'Sheet1') #通過名稱獲取

3.讀取行和列

sheet0.row_values(0) #讀取第一行

sheet0.col_values(0) #讀取第一列

4.獲取行數(shù)和列數(shù)

nrows = sheet0.nrows #獲取行數(shù)

ncols = sheet0.ncols #獲取列數(shù)

5.讀取單元格

cell_A1 = sheet0.cell(0,0).value

cell_A1 = sheet0.row(0)[0].value

cell_B4 = sheet0.cell(3,1).value

?

xlwt

1.新建一個excel文件 file = xlwt.Workbook()

2. 新建一個sheet sheet0= file.add_sheet('sheet name')

3.寫入數(shù)據(jù)

table.write(行,列,value) sheet0.write(0,0,‘第一行,第一列(A1)'.decode('utf-8'))

sheet0.write(3,1,‘第四行,第二列(B4)'.decode('utf-8')) 保存文件 File.save('demo.xls')

4.xlwt樣式

修改字體

style = xlwt.XFStyle() # 初始化樣式 font = xlwt.Font() #為樣式創(chuàng)建字體 font.name = 'Times New Roman' font.bold = True style.font = font #為樣式設(shè)置字體 table.write(0, 1, 'some bold Times text', style) # 使用樣式

更多樣式的說明可以參考: http://blog.sina.com.cn/s/blog_5357c0af01019gjo.html

?

xlutils

復(fù)制excel對象

wb = copy(r_file)

獲取工作表

sheet0=wb.get_sheet(0)

保存

wb.save('demo.xls')

?

copy有一個缺點(diǎn),就是copy后會把樣式格式化,我們可以寫一個copy2,來讓copy后的文件保留原有的樣式

def copy2(r_file):'''附帶樣式的copy
  xlrd打開文件,必須加參數(shù)formatting_info=True
'''w = XLWTWriter()process(XLRDReader(r_file,'unknown.xls'),w)r_sheets=r_file.sheets()w_file, style_list = w.output[0][1], w.style_listfor index,r_sheets in enumerate(r_sheets):w_sheet = w_file.get_sheet(index)rows = r_sheets.nrows #獲取行數(shù)cols = r_sheets.ncols #獲取列數(shù)for row in range(rows):for col in range(cols):xf_index = r_sheets.cell_xf_index(row, col)value=r_sheets.cell(row,col).valuew_sheet.write(row, col, value, style_list[xf_index])return w_file,style_list

?

畫邊框的函數(shù)

def draw_border(r_sheet, w_sheet, left_top, right_bottom, style_list, border_type_index=5, border_color=0x40):'''@r_sheet:workbook 讀取的sheet@w_sheet:workbook 寫入的sheet@left_top:tuple 邊框的左上角的坐標(biāo),如 (0,1)@right_bottom:tuple 邊框的右下角的坐標(biāo),如 (10,5)@style_list : 讀取的sheet的樣式列表,通過copy2方法獲取@border_type_index:int 邊框的樣式的下標(biāo)@border_color:int 邊框的顏色return 1'''import xlwtborder_types = ['NO_LINE', 'THIN', 'MEDIUM', 'DASHED', 'DOTTED', 'THICK', 'DOUBLE']border_type = border_types[border_type_index % len(border_types)]border_type = getattr(xlwt.Borders, border_type)def _get_border(type, color, top=0, right=0, bottom=0, left=0):border = xlwt.Borders()for direct in ('top', 'right', 'bottom', 'left'):if locals().get(direct):setattr(border, direct, type)setattr(border, direct + '_colour', color)return borderdef _draw_boder(row, col, border):try:style_index = r_sheet.cell_xf_index(row, col)style = style_list[style_index]value = r_sheet.cell(row, col).valueexcept:style = xlwt.XFStyle()value = ''style.borders = borderw_sheet.write(row, col, value, style)if left_top > right_bottom:left_top, right_bottom = right_bottom, left_topleft, top, right, bottom = left_top + right_bottomfor row in range(top, bottom + 1):for col in range(left, right + 1):left_ = 1 if col == left else 0top_ = 1 if row == top else 0right_ = 1 if col == right else 0bottom_ = 1 if row == bottom else 0border = _get_border(border_type, border_color, top_, right_, bottom_, left_)_draw_boder(row, col, border)return 1

調(diào)用方法

import xlrd r_file= xlrd.open_workbook('service_base.xls', formatting_info=True) r_sheet=r_file.sheets()[0] w_file,style_list =copy2(r_file) w_sheet=w_file.get_sheet(0) draw_border(r_sheet,w_sheet,(5,5),(0,0),style_list)w_file.save('service_base1.xls')

?

?

以下是xlrd+xlwt+xlutils組合的代碼:

#encoding=utf-8 import xlrd import xlwt from xlutils.copy import copydef getLastDay(year,month):if month in ["1","3","5","7","8","10","12"]:return "31"elif month=="2":if year in ["2008","2012"]:return "29"else: return "28"else:return "30"rFile =xlrd.open_workbook("sb1.xls") #for i in range(2006,2014): # for j in range(1,13): if 1:i=2006j=2dateText="所得期間: %s年 %s月"%(i,j)dateText=dateText.decode("utf-8")dateStar="%s-%s-1"%(i,j)dateEnd="%s-%s-%s"%(i,j,getLastDay(i,j))wFile=copy(rFile)sheet1=wFile.get_sheet(0)sheet1.write(3,11,dateText)sheet1.write(11,5,dateStar)sheet1.write(11,6,dateEnd)#wFile.save("sb1.xls") wFile.save("sb1%s-%s.xls"%(i,j))

?在保存xls文件時,報錯:'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)

解決方法是write的內(nèi)容都decode一下

sheet1.write(4+i,j+1,'你好'.decode('utf-8'))

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/Xjng/p/3524901.html

總結(jié)

以上是生活随笔為你收集整理的Python操作Excel——win32com模块和xlrd+xlwt+xlutils组合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。