python使用openpyxl庫修改excel表格數(shù)據(jù)方法
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)
簡單使用:
1、新建excel文件處理
wb = Workbook()#創(chuàng)建工作簿
ws = wb.active#激活工作表
ws1 = wb.create_sheet("Mysheet")#創(chuàng)建mysheet表
ws.title = "New Title"#表明改為New Title
ws.sheet_properties.tabColor = "1072BA"#顏色
ws['A4'] = 4#賦值
d = ws.cell(row=4, column=2, value=10)#賦值
cell_range = ws['A1':'C2']#選擇單元格區(qū)域
wb.save('test.xlsx')#保存
2、已有excel文件的處理
a、修改excel數(shù)據(jù)
wr = Write_excel('d:\demo.xlsx')
wr.write('A2','hello')
b、合并單元格
wr.merge('A1:B3')
c、單元格加入樣式,如字體,顏色等屬性
單元格B2設(shè)置宋體,14號,紅色,自動換行,水平居中,垂直居中
font = Font(name=u'宋體', size=14, color=RED, bold=True)
align = Alignment(horizontal='center', vertical='center')
wr.cellstyle('B2', font, align)
d、創(chuàng)建3d柱狀圖
rows = [
(None, 2013, 2014),
("Apples", 5, 4),
("Oranges", 6, 2),
("Pears", 8, 3)
]
for row in rows:
ws.append(row)
wr.makechart(u"3D Bar Chart", 'E5', 12.5, 7, 2, 1, 3, 4, 1, 2, 4)

可以創(chuàng)建3d柱狀和折線圖表,挺好用的。
官方文檔:https://openpyxl.readthedocs.io/en/latest/usage.html
以上這篇python使用openpyxl庫修改excel表格數(shù)據(jù)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中的NumPy實(shí)用函數(shù)整理之percentile詳解
這篇文章主要介紹了Python中的NumPy實(shí)用函數(shù)整理之percentile詳解,NumPy函數(shù)percentile()用于計(jì)算指定維度上數(shù)組元素的第?n?個百分位數(shù),返回值為標(biāo)量或者數(shù)組,需要的朋友可以參考下2023-09-09
Python 中eval()函數(shù)的正確使用及其風(fēng)險(xiǎn)分析(使用示例)
eval()是一個功能強(qiáng)大的工具,但使用時必須非常小心,了解其工作原理和潛在的風(fēng)險(xiǎn)是確保安全使用的關(guān)鍵,通過遵循上述建議,可以在享受eval()帶來的便利的同時,最大限度地減少安全風(fēng)險(xiǎn),本文介紹Python 中`eval()`函數(shù)的正確使用及其風(fēng)險(xiǎn)分析,感興趣的朋友一起看看吧2024-07-07
使用python處理題庫表格并轉(zhuǎn)化為word形式的實(shí)現(xiàn)
這篇文章主要介紹了使用python處理題庫表格并轉(zhuǎn)化為word形式的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python使用pyautocad+openpyxl處理cad文件示例
這篇文章主要介紹了Python使用pyautocad+openpyxl處理cad文件,結(jié)合實(shí)例形式分析了Python使用pyautocad與openpyxl模塊讀寫cad文件相關(guān)應(yīng)用操作技巧,需要的朋友可以參考下2019-07-07

