python使用openpyxl库修改excel表格数据方法


本文摘自php中文网,作者不言,侵删。

这篇文章主要介绍了关于使用python使用openpyxl库修改excel表格数据方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

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

简单封装类:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

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面向对象之继承和多态

Python中迭代器与迭代器切片的详细介绍

计算机二级考试有Python

Python编写一个三级while的循环菜单实例

Python环境变量如何配置

Python换行符是什么

Python全栈是什么意思

Python中怎么安装pip工具?

Python实现合并同一个文件夹下所有pdf文件的方法示例

Python中格式化字符串有什么方法?(对比介绍)

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...