python使用openpyxl库修改excel表格数据方法-创新互联

1、openpyxl库可以读写xlsx格式的文件,对于xls旧格式的文件只能用xlrd读,xlwt写来完成了。

创新互联公司是一家以成都网站建设、网页设计、品牌设计、软件运维、成都网站营销、小程序App开发等移动开发为一体互联网公司。已累计为砂岩浮雕等众行业中小客户提供优质的互联网建站和软件开发服务。

简单封装类:

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表格数据方法-创新互联
新闻来源:http://ybzwz.com/article/dggdpc.html