python针对excel的读写操作-----openpyxl
Posted 王绵绵
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python针对excel的读写操作-----openpyxl相关的知识,希望对你有一定的参考价值。
#python读取excel from openpyxl import load_workbook wb = load_workbook(\'test.xlsx\') print(wb.sheetnames)
from openpyxl import load_workbook wb = load_workbook(\'test.xlsx\') sheet = wb.get_sheet_by_name(\'Sheet1\') print(sheet[\'A\'])#列表A的所有数据
>>>(<Cell \'Sheet1\'.A1>, <Cell \'Sheet1\'.A2>, <Cell \'Sheet1\'.A3>, <Cell \'Sheet1\'.A4>, <Cell \'Sheet1\'.A5>, <Cell \'Sheet1\'.A6>, <Cell \'Sheet1\'.A7>, <Cell \'Sheet1\'.A8>, <Cell \'Sheet1\'.A9>, <Cell \'Sheet1\'.A10>)
print(sheet[\'4\'])#列表第四行的所有内容
>>>(<Cell \'Sheet1\'.A4>, <Cell \'Sheet1\'.B4>, <Cell \'Sheet1\'.C4>, <Cell \'Sheet1\'.D4>, <Cell \'Sheet1\'.E4>)
print(sheet[\'C4\'].value)
>>>哎哟哇
for i in sheet[\'C\']:
print(i.value)
>>>c1
c2
c3
哎哟哇
c5
c6
c7
c8
c9
c10
#python 写入excel from openpyxl import Workbook wb = Workbook() sheet = wb.active sheet.title = \'New sheet\'#定义sheet的标题,下面的sheet名字 sheet[\'C3\'] = \'hello\'#C列,第三行,写入hello for i in range(10): sheet[\'A%d\' %(i+1)].value = i+1#循环遍历0到9,然后i+1 就是1到10,这句话表示A1-A10,写入1-10 sheet[\'E1\'].value = \'=SUM(A:A)\'#在E1写入A列所有数据的和 wb.save(\'保存一个新的excel.xlsx\')#保存到一个新的excel
以上是关于python针对excel的读写操作-----openpyxl的主要内容,如果未能解决你的问题,请参考以下文章