1、openpyxl庫可以讀寫xlsx格式的文件,對于xls舊格式的文件只能用xlrd讀,xlwt寫來完成了。
簡單封裝類:
from openpyxl import load_workbook from openpyxl import Workbook from openpyxl.chart import BarChart, Series, Reference, BarChart3D from openpyxl.styles import Color, Font, Alignment from openpyxl.styles.colors import BLUE, RED, GREEN, YELLOW class Write_excel(object): def __init__(self,filename): self.filename = filename self.wb = load_workbook(self.filename) self.ws = self.wb.active def write(self, coord, value): # eg: coord:A1 self.ws.cell(coord).value = value self.wb.save(self.filename) def merge(self, rangstring): # eg: rangstring:A1:E1 self.ws.merge_cells(rangstring) self.wb.save(self.filename) def cellstyle(self, coord, font, align): cell = self.ws.cell(coord) cell.font = font cell.alignment = align def makechart(self, title, pos, width, height, col1, row1, col2, row2, col3, row3, row4): ''':param title:圖表名 pos:圖表位置 width:圖表寬度 height:圖表高度 ''' data = Reference(self.ws, min_col=col1, min_row=row1, max_col=col2, max_row=row2) cat = Reference(self.ws, min_col=col3, min_row=row3, max_row=row4) chart = BarChart3D() chart.title = title chart.width = width chart.height = height chart.add_data(data=data, titles_from_data=True) chart.set_categories(cat) self.ws.add_chart(chart, pos) self.wb.save(self.filename)
本文名稱:python使用openpyxl庫修改excel表格數(shù)據(jù)方法-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://newbst.com/article32/dggdpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、關(guān)鍵詞優(yōu)化、虛擬主機、網(wǎng)站導(dǎo)航、品牌網(wǎng)站建設(shè)、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容