在练习使用Python将MySQL数据库表数据导入excel中时,程序以及编写好如下了
import pymysql
import openpyxl
workbook = openpyxl.Workbook
sheet = workbook.active
sheet.title = '学生表'
sheet.append(('学号', '姓名', '头衔', '学院'))
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='zl123456', database='chat',
charset='utf8mb4')
try:
with conn.cursor() as c:
c.execute('select * from `tb_teacher`')
row = c.fetchone()
while row:
sheet.append(row)
row = c.fetchone()
workbook.save('student.xlsx')
except pymysql.MySQLError as e:
print(e)
finally:
conn.close()
但是运行却老是报错误
AttributeError: 'property' object has no attribute 'append'
AttributeError: 'property' object has no attribute 'title'
...
仔细检查后发现在创建工作簿对象时出了问题 就在图片中划线部分,忘记在末尾加括号了,所以应该是
workbook = openpyxl.Workbook()
这样就可以了
|