python ----------操作excel文件
安装相应的库
pip install xlrd xlwt xlutils 可以兼容低版本的excel文件 pip install openpyxl 2007及以后的excel版本
用xlutils来读写excel
注意:只能用xlrd 来读取数据,然后用xlwt 来写excel表格
读取相关数据
import xlrd
wb = xlrd.open_workbook('resourse/阿里巴巴2020年股票数据.xlsx')
print(wb.sheet_names())
sheet = wb.sheet_by_index(0)
print(sheet.nrows, sheet.ncols)
print(sheet.row(0))
print(sheet.row_slice(0, start_colx=0, end_colx=3))
print(sheet.col(4))
print(sheet.col(4, start_rowx=1, end_rowx=3))
cell = sheet.cell(2, 2)
print(cell.value)
sheet.cell(row,col) 作为读 指定的某一个单元格
遍历整个表单
for row in range(1, sheet.nrows):
for col in range(sheet.ncols):
value = sheet.cell(row, col).value
if col == 0:
curr_date = xlrd.xldate_as_datetime(value, 0)
print(curr_date.strftime('%Y年%m月%d日'), end='\t')
elif col == 5:
print(f'{int(value):<10d}', end='\t')
else:
print(f'{value:.2f}', end='\t')
print()
关于日期的操作
from datetime import datetime
date1 = datetime(1998, 5, 4)
print(date1)
date2 = datetime(1998, 6, 8, 23, 58, 13)
print(date2)
date3 = datetime.now()
print(date3)
delta = date3 - date1
print(delta, type(delta))
print(delta.days, delta.seconds)
print(date2.strftime('%Y年%m月%d日 %H时%M分%S秒'))
写excel文件
用sheet.write(row, col, 内容) 来写excel 步骤
- 第一步:创建工作簿
- 第二步:添加工作表
- 第三步:向单元格写入数据
- 第四步:保存工作簿
import random
import xlwt
wb = xlwt.Workbook()
sheet = wb.add_sheet('期末成绩')
sheet.write(0, 0, '姓名')
sheet.write(0, 1, '语文')
sheet.write(0, 2, '数学')
sheet.write(0, 3, '英语')
names= ('小红', '小米', '小黑', '小张', '小贺')
for i, name in enumerate(names):
sheet.write(i+1, 0, name)
for i in range(1, 6):
for k in range(1, 4):
x = random.randrange(50, 101)
sheet.write(i, k, x)
wb.save('resourse/学生成绩表.xls')
用openpyxl来读写excel
此方法,用sheet.cell(row,col) 来获取单元格内容 用sheet.cell(row,col,内容) 来写单元格的内容
读excel
import datetime
import openpyxl
wb = openpyxl.load_workbook('resourse/阿里巴巴2020年股票数据.xlsx')
print(type(wb))
print(wb.sheetnames)
sheet = wb.worksheets[0]
print(type(sheet))
print(sheet.dimensions)
print(sheet.max_row, sheet.max_column)
for row in range(2, sheet.max_row + 1):
for col in range(1, sheet.max_column + 1):
value = sheet.cell(row, col).value
if col == 1:
print(value.strftime('%Y年%m月%d日'), end='\t')
elif col == 6:
print(f'{int(value):<10d}', end='\t')
else:
print(f'{value:.2f}', end='\t')
print()
写excel
import random
import openpyxl
wb = openpyxl.Workbook()
sheet = wb.active
sheet.title = '期末成绩'
sheet.cell(1, 1, '姓名')
sheet.cell(1, 2, '语文')
sheet.cell(1, 3, '数学')
sheet.cell(1, 4, '英语')
names= ('小红', '小米', '小黑', '小张', '小贺')
for i, name in enumerate(names):
sheet.cell(i+2, 1, name)
for i in range(2, 7):
for k in range(2, 5):
x = random.randrange(50, 101)
sheet.cell(i, k, x)
wb.save('resourse/学生成绩表.xlsx')
关于表格的特殊格式
以下方法可以修改单元格的背景色,可以修改字体,还有对齐方式
header_style = xlwt.XFStyle()
header_pattern = xlwt.Pattern()
header_pattern.pattern = xlwt.Pattern.SOLID_PATTERN
header_pattern.pattern_fore_colour = xlwt.Style.colour_map['aqua']
header_style.pattern = header_pattern
header_font = xlwt.Font()
header_font.height = 20 * 22
header_font.bold = True
header_style.font = header_font
header_alignment = xlwt.Alignment()
header_alignment.vert = xlwt.Alignment.VERT_CENTER
header_alignment.horz = xlwt.Alignment.HORZ_CENTER
header_style.alignment = header_alignment
|