openpyxl

写入

from openpyxl import Workbook


wb = Workbook()

wb1 = wb.create_sheet('index', 0)
wb1['D2'] = 'alsidhf'
wb1.cell(4,2,'11111111111')

wb1['A1'] = 4
wb1['A2'] = 3
wb1['A3'] = '=sum(A1:A2)'

wb1.append([])
wb1.append([])
wb1.append([])
wb1.append([])
wb1.append([])
wb1.append([1,2,3,4,5,6,7,8,9])
wb1.title = 'user'
wb.save('a.xlsx')

读取

from openpyxl import load_workbook


wb = load_workbook('a.xlsx')
print(wb.sheetnames)

wb1 = wb['user']
print(wb1['A2'].value)

print(wb1.cell(2,1).value)

print(wb1.max_row)
print(wb1.max_column)

print(wb1.rows)
print(wb1.columns)
a = wb1.rows
b = wb1.columns
print(type(a), a)
print('#############')
qq = a.__next__()
print(qq)
print(a.__next__())
print(a.__next__())
print(a.__next__())
print(a.__next__())
print(a.__next__())
print(a.__next__())
print(b)

print('#############')

print([el.value for el in qq])
# 输出
['user', 'Sheet'] 3 3 10 9 <generator object Worksheet._cells_by_row at 0x000001569253E570> <generator object Worksheet._cells_by_col at 0x000001569253E570> <class 'generator'> <generator object Worksheet._cells_by_row at 0x000001569253E570> ############# (<Cell 'user'.A1>, <Cell 'user'.B1>, <Cell 'user'.C1>, <Cell 'user'.D1>, <Cell 'user'.E1>, <Cell 'user'.F1>, <Cell 'user'.G1>, <Cell 'user'.H1>, <Cell 'user'.I1>) (<Cell 'user'.A2>, <Cell 'user'.B2>, <Cell 'user'.C2>, <Cell 'user'.D2>, <Cell 'user'.E2>, <Cell 'user'.F2>, <Cell 'user'.G2>, <Cell 'user'.H2>, <Cell 'user'.I2>) (<Cell 'user'.A3>, <Cell 'user'.B3>, <Cell 'user'.C3>, <Cell 'user'.D3>, <Cell 'user'.E3>, <Cell 'user'.F3>, <Cell 'user'.G3>, <Cell 'user'.H3>, <Cell 'user'.I3>) (<Cell 'user'.A4>, <Cell 'user'.B4>, <Cell 'user'.C4>, <Cell 'user'.D4>, <Cell 'user'.E4>, <Cell 'user'.F4>, <Cell 'user'.G4>, <Cell 'user'.H4>, <Cell 'user'.I4>) (<Cell 'user'.A5>, <Cell 'user'.B5>, <Cell 'user'.C5>, <Cell 'user'.D5>, <Cell 'user'.E5>, <Cell 'user'.F5>, <Cell 'user'.G5>, <Cell 'user'.H5>, <Cell 'user'.I5>) (<Cell 'user'.A6>, <Cell 'user'.B6>, <Cell 'user'.C6>, <Cell 'user'.D6>, <Cell 'user'.E6>, <Cell 'user'.F6>, <Cell 'user'.G6>, <Cell 'user'.H6>, <Cell 'user'.I6>) (<Cell 'user'.A7>, <Cell 'user'.B7>, <Cell 'user'.C7>, <Cell 'user'.D7>, <Cell 'user'.E7>, <Cell 'user'.F7>, <Cell 'user'.G7>, <Cell 'user'.H7>, <Cell 'user'.I7>) <generator object Worksheet._cells_by_col at 0x000001569253E780> ############# [4, None, None, None, None, None, None, None, None]
原文地址:https://www.cnblogs.com/NachoLau/p/10420145.html