验证中...
Languages: Java
Categories: 微信开发
Latest update 2019-09-11 15:46
1.py
Raw Copy
import urllib.request
response=urllib.request.urlopen("http://placekitten.com/600/400")
cat_img=response.read()
with open('cat2.jpg','wb') as f:
f.write(cat_img)
print("下载完成!")
2.py
Raw Copy
from xlwt import *
book = Workbook() #新建workbook
sheet1 = book.add_sheet("First") #新建worksheet
al=Alignment()
al.horz=Alignment.HORZ_CENTER
al.vert=Alignment.VERT_CENTER #对齐方式
borders=Borders()
borders.bottom=Borders.THICK #边框样式
style=XFStyle()
style.alignment=al
style.borders=borders
row0=sheet1.row(0)
row0.write(0,'张岩',style=style) #写入单元格
book.save(r'd:\test.xls') #保存文件
xls1.py
Raw Copy
from xlwt import *
book = Workbook() #新建workbook
sheet1 = book.add_sheet("First") #新建worksheet
al=Alignment()
al.horz=Alignment.HORZ_CENTER
al.vert=Alignment.VERT_CENTER #对齐方式
borders=Borders()
borders.bottom=Borders.THICK #边框样式
style=XFStyle()
style.alignment=al
style.borders=borders
row0=sheet1.row(1)
row0.write(2,'test',style=style) #写入单元格
book.save(r'd:\test.xls') #保存文件
xls2.py
Raw Copy
import xlrd
book = xlrd.open_workbook(r'd:\test.xls')
sheet1 = book.sheet_by_name('First')
row0 = sheet1.row(0)
print(row0[0])
print(row0[0].value)
xls3.py
Raw Copy
from random import choice, randint
from openpyxl import Workbook, load_workbook
#生成随机数据
def generateRandomInformation(filename):
workbook = Workbook()
worksheet = workbook.worksheets[0]
worksheet.append(['姓名','课程','成绩'])
#中文名字中的第一、第二、第三个字
first = '赵钱孙李'
middle = '伟昀琛东'
last = '坤艳志'
subjects = ('语文','数学','英语')
for i in range(200):
name = choice(first)
#按一定概率生成只有两个字的中文名字
if randint(1,100)>50:
name = name + choice(middle)
name = name + choice(last)
#依次生成姓名、课程名称和成绩
worksheet.append([name, choice(subjects), randint(0, 100)])
#保存数据,生成Excel 2007格式的文件
workbook.save(filename)
def getResult(oldfile, newfile):
#用于存放结果数据的字典
result = dict()
#打开原始数据
workbook = load_workbook(oldfile)
worksheet = workbook.worksheets[0]
#遍历原始数据
for row in worksheet.rows:
if row[0].value == '姓名':
continue
#姓名,课程名称,本次成绩
name, subject, grade = map(lambda cell:cell.value, row)
#获取当前姓名对应的课程名称和成绩信息
#如果result字典中不包含,则返回空字典
t = result.get(name, {})
#获取当前学生当前课程的成绩,若不存在,返回0
f = t.get(subject, 0)
#只保留该学生该课程的最高成绩
if grade > f:
t[subject] = grade
result[name] = t
workbook1 = Workbook()
worksheet1 = workbook1.worksheets[0]
worksheet1.append(['姓名','课程','成绩'])
#将result字典中的结果数据写入Excel文件
for name, t in result.items():
print(name, t)
for subject, grade in t.items():
worksheet1.append([name, subject, grade])
workbook1.save(newfile)
if __name__ == '__main__':
oldfile = r'd:\test.xlsx'
newfile = r'd:\result.xlsx'
generateRandomInformation(oldfile)
getResult(oldfile, newfile)

Comment list( 0 )

You need to Sign in for post a comment

Help Search