day7_操作excel的三种方式

 一、写入excel

写入excel首先要安装xlwt,安装xlwt的命令是pip install xlwt,安装成功后导入xlwt到pycharm里
1、写入excel的第一种实现方式的代码如下(这种方式适合写入一行内容或行数有限的时候用)

import xlwt
book = xlwt.Workbook() # 创建一个excel
sheet = book.add_sheet('sheet1') # 添加一个sheet页,也可以把sheet1改成别的名字
sheet.write(0, 0, '姓名') # 0行,0列,写入姓名---写入表头,就是excel的第一行内容
sheet.write(0, 1, '年龄') # 0行,1列,写入年龄---写入表头,就是excel的第一行内容
sheet.write(0, 2, '身高') # 0行,2列,写入身高---写入表头,就是excel的第一行内容
sheet.write(1, 0, '孙树江') # 1行,0列,写入内容
sheet.write(1, 1, 22) # 1行,1列,写入内容
sheet.write(1, 2, '175cm') # 1行,2列,写入内容
book.save('write.xls')

2、写入excel的第二种实现方式的代码如下(这种方式适合写入几行内容的时候用):

import xlwt
book = xlwt.Workbook() # 创建一个excel
sheet = book.add_sheet('sheet1') # 添加一个sheet页,也可以把sheet1改成别的名字
title = ['姓名', '班级', '住址', '手机号'] # 写入的表头存放在一个list里面
# 写入的内容写入一个二维list里面
data = [
['杨建波', '巨蟹座', '沙河', 110],
['王俊熹 ', '巨蟹座', '昌平', 120],
['张丹丹', '巨蟹座', '西二旗', 122],
['侯雪梅', '巨蟹座', '望京', 0]
]
i = 0 # 控制列,写入每一行内容,列变行不变
for j in title:
sheet.write(0, i, j) # 把表头信息写进去
i = i + 1 # 列数加1
line = 1 # 控制行,行变列不变
for d in data:
sheet.write(line, 0, d[0]) # 把内容都写到excel里面,d代表data中每个list
sheet.write(line, 1, d[1]) # 把内容都写到excel里面,d代表data中每个list
sheet.write(line, 2, d[2]) # 把内容都写到excel里面,d代表data中每个list
sheet.write(line, 3, d[3]) # 把内容都写到excel里面,d代表data中每个list
line = line + 1 # 行数加1
book.save('ssj.xls') # 保存写入后的内容,后缀只能是xls,要不然打不开

3、写入excel的第三种实现方式的代码如下(这种方式没写表头,只写了内容,前两种方式不是很实用,第三种方式采用循环,适合写入多行的数据,比较常用):

import xlwt
book = xlwt.Workbook() # 创建一个excel
sheet = book.add_sheet('sheet1') # 添加一个sheet页,也可以把sheet1改成别的名字
# 写入的内容写入一个二维list里面
data = [
['高大伟', '巨蟹座', '沙河', 110],
['王景龙 ', '巨蟹座', '昌平', 120],
['张丹丹', '巨蟹座', '西二旗', 122],
['张名媛', '巨蟹座', '望京', 0]
]
row = 1 # 第一行写入内容
for d in data: # d代表每行,一个list,循环每行
col = 0 # 第0列
for dd in d: # dd代表每个list里面的每个元素
sheet.write(row, col, dd) # 循环每个list里的每一个元素
col = col + 1 # 列数加1
row = row + 1
book.save('ssj.xls') # 后缀只能是xls,要不然打不开

二、读出excel中的数据

读excel首先要安装xlrd,安装xlrd的命令是pip install xlrd,安装成功后导入xlrd到pycharm里
读出excel中的数据实现方式的代码如下:

def con_mysql(sql):
import pymysql
conn = pymysql.connect(host='211.149.218.16', user='jxz', password='123456', db='jxz', charset='utf8')
cur = conn.cursor()
cur.execute(sql)
res = cur.fetchall()
cur.close()
conn.close()
return res
def is_send(a): # 判断是否交作业了
if a:
return '已交作业'
else:
return '未交作业'
def readexcel(filename):
import xlrd
book = xlrd.open_workbook(filename) # 打开指定的excel表
sheet = book.sheet_by_name('Sheet1') # 读取Sheet1页的内容
for i in range(sheet.nrows): # sheet.nrows代表excel里面的行数
lines = sheet.row_values(i) # 每行的内容
name = lines[0]
c1 = is_send(lines[1])
c2 = is_send(lines[2])
c3 = is_send(lines[3])
sql = 'insert into jxz_stu values("{name}","{c1}","{c2}","{c3}")'.format(name=name, c1=c1, c2=c2, c3=c3)
con_mysql(sql)
readexcel('stu.xls')

三、修改excel
修改excel里面的内容,首先要安装xlutils,安装xlutils的命令是pip install xlutils,安装成功后导入xlutils到pycharm里
修改excel中的内容实现方式的代码如下:

from xlutils.copy import copy
import xlrd
import os
book = xlrd.open_workbook('stu.xls')  # 打开一个要修改的excel
new_book = copy(book)  # 复制一份原来的excel
sheet = new_book.get_sheet(0)  # 获取到新的excel里面的第一个sheet页
sheet.write(12, 0, '孙树江')  # 修改12行0列的文件内容
new_book.save('stu_new.xls')  # 保存新的excel
os.remove('stu.xls')  # 删除以前的excel文件
os.rename('stu_new.xls', 'stu.xls')  # 把新的文件重命名为原来的excel文件

原文地址:https://www.cnblogs.com/laosun0204/p/7847003.html