使用Python进行Excel写入与读取
本地安装openpyxl模块
安装方法:window电脑:在终端输入命令:pip install openpyxl,按下enter键;mac电脑:在终端输入命令:pip3 install openpyxl,按下enter键
具体实现
import openpyxl #写入的代码:
wb = openpyxl.Workbook() sheet = wb.active sheet.title = ‘new title’ sheet[‘A1’] = ‘学生表’ rows = [[‘姓名’,‘学号’,‘年龄’,‘学科’,‘成绩’],[‘张三’,‘001’,‘18’, ‘语文’,‘95’]] for i in rows: sheet.append(i) print(rows) wb.save(‘student.xlsx’) 读取的代码: wb = openpyxl.load_workbook(‘Marvel.xlsx’) sheet = wb[‘new title’] sheetname = wb.sheetnames print(sheetname) A1_cell = sheet[‘A1’] A1_value = A1_cell.value print(A1_value)
|