python处理excel表格——openpyxl
openpyxl为三方库,需要 pip install openpyxl安装。
一、标准使用方式:
import os
import openpyxl
student_info = [{"name":"Jack", "gender":"man"}, {"name":"Mary", "gender":"woman"}]
excel_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'output/student.xlsx')
book = openpyxl.Workbook()
sheet = book.create_sheet('base_info')
book.save("student.xlsx")
book = openpyxl.load_workbook("student.xlsx")
sheet = book['base_info']
rows = sheet.max_row
columns = sheet.max_column
print(rows, columns)
for i in range(len(student_info)):
sheet.cell(row=i+1, column=1).value = student_info[i]['name']
sheet.cell(row=i+1, column=2).value = student_info[i]['gender']
book.save(excel_file)
print("create excel successful!")
![在这里插入图片描述](https://img-blog.csdnimg.cn/5982bf18491e4d579de206d1355b0415.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNDk0ODcz,size_16,color_FFFFFF,t_70)
|