from . import web_blue
import xlrd
@web_blue.route('/pexcel')
def pexcel():
# 打开文件
data = xlrd.open_workbook(r'C:\Users\mei\Desktop\教师.xls')#文件的路径
table = data.sheets()[0]#索引从0开始 代表sheets1
nrows=table.nrows#行数
ncols=table.ncols#列数
row_list = [table.row_values(i) for i in range(0, nrows)] # 所有行的数据
col_list = [table.col_values(i) for i in range(0, ncols)] # 所有列的数据
print('每行数据:',row_list)
print('每列数据',col_list)
return 'hello'
输出内容:
每行数据: [[‘账号’, ‘姓名’], [111.0, ‘评委一’], [222.0, ‘评委二’], [333.0, ‘评委三’], [444.0, ‘评委四’], [555.0, ‘评委五’], [666.0, ‘评委六’], [777.0, ‘评委七’], [888.0, ‘评委八’], [999.0, ‘评委九’]]
每列数据 [[‘账号’, 111.0, 222.0, 333.0, 444.0, 555.0, 666.0, 777.0, 888.0, 999.0], [‘姓名’, ‘评委一’, ‘评委二’, ‘评委三’, ‘评委四’, ‘评委五’, ‘评委六’, ‘评委七’, ‘评委八’, ‘评委九’]]
注意: 如果增加了一个sheet表 只需要将sheets()[]索引改变即可。
import requests
from flask import request
from app.libs.redprint import Redprint
import xlrd
api = Redprint('voter')
@api.route('/addlist',methods = ['POST'])
def voter_post():
data = request.get_json()#表单中存了json格式url 转化为 字典
Download_url = https://masterproject.oss-cn-beijing.aliyuncs.com/%E6%95%99%E5%B8%88%20%282%29.xls?Expires=1560134985&OSSAccessKeyId=TMP.AgGMj8iFu8pjSIvzZHfBmo8KBzL19JGLvPUm-hPcsrKdqWgIEiXc80poftx2ADAtAhUAssyG_cm1ZQb53OwI94cX-8tW7wYCFFMR15xqGpW9CxRpszFw7700MeWz&Signature=8rHR8DHMk8TNpMbdbMv91wIa9TQ%3D#url是下载路径
r = requests.get(Download_url)
f = open("教师3.xls","wb")#项目路径下新增一个教师.xls文件
f.write(r.content)
f.close()
注意:此方法是将内容读取在写入excel中。更好的方法是使用urllib这个模块。
import urllib.request
#根据url下载文件
url = "https://masterproject.oss-cn-beijing.aliyuncs.com/%E6%95%99%E5%B8%88%20%282%29.xls?Expires=1560134985&OSSAccessKeyId=TMP.AgGMj8iFu8pjSIvzZHfBmo8KBzL19JGLvPUm-hPcsrKdqWgIEiXc80poftx2ADAtAhUAssyG_cm1ZQb53OwI94cX-8tW7wYCFFMR15xqGpW9CxRpszFw7700MeWz&Signature=8rHR8DHMk8TNpMbdbMv91wIa9TQ%3D"
downPath = '..\\teacher.xls'#相对路径
urllib.request.urlretrieve(url,downPath)