Python: read/write excel

# 读写2003 excelimport xlrdimport xlwt

# 读写2007 excelimport openpyxl

def write03Excel(path):

wb = xlwt.Workbook()

sheet = wb.add_sheet("2003测试表")

value = [["名称", "价格", "出版社", "语言"],

["如何高效读懂一本书", "22.3", "机械工业出版社", "中文"],

["暗时间", "32.4", "人民邮电出版社", "中文"],

["拆掉思维里的墙", "26.7", "机械工业出版社", "中文"]]

for i in range(0, 4):

for j in range(0, len(value[i])):

sheet.write(i, j, value[i][j])

wb.save(path)

print("写入数据成功!")

def read03Excel(path):

workbook = xlrd.open_workbook(path)

sheets = workbook.sheet_names()

worksheet = workbook.sheet_by_name(sheets[0])

for i in range(0, worksheet.nrows):

row = worksheet.row(i)

for j in range(0, worksheet.ncols):

print(worksheet.cell_value(i, j), "\t", end="")

print()

def write07Excel(path):

wb = openpyxl.Workbook()

sheet = wb.active

sheet.title = '2007测试表'

value = [["名称", "价格", "出版社", "语言"],

["如何高效读懂一本书", "22.3", "机械工业出版社", "中文"],

["暗时间", "32.4", "人民邮电出版社", "中文"],

["拆掉思维里的墙", "26.7", "机械工业出版社", "中文"]]

for i in range(0, 4):

for j in range(0, len(value[i])):

sheet.cell(row=i+1, column=j+1,

value=str(value[i][j]))

wb.save(path)

print("写入数据成功!")

def read07Excel(path):

wb = openpyxl.load_workbook(path)

sheet = wb.get_sheet_by_name('2007测试表')

for row in sheet.rows:

for cell in row:

print(cell.value, "\t", end="")

print()

file_2003 = 'data/2003.xls'

file_2007 = 'data/2007.xlsx'

write03Excel(file_2003)

read03Excel(file_2003)

write07Excel(file_2007)

read07Excel(file_2007)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值