使用python对excel文件操作保存后(save函数)文件被破坏解决办法

今日进行了利用python对excel的文本操作,这方面的文章网上已经有很多了,在此不再赘述,只要使用几个库就行了:

import xlrd
import xlwt
from datetime import date,datetime
from xlutils.copy import copy

然后使用其中的函数即可对excel文件进行读、写、修改、保存等诸多功能,十分方便。
但是今日在利用“from xlutils.copy import copy”保存excel文件后,遇到了文件无法再次打开的问题,并报错。但是重新运行python代码确可以读取并再次操作其中的数据,因此文件肯定还是好的,一定是格式出现了错误,后来经过调试及搜索,果然是格式的问题,save函数只能将excel文件保存成.xls文件,而代码却将其保存成.xlsx文件,因此格式不对,无法打开,将其修改后成功打开,并且前期对excel内容的操作也都十分成功,数据已经成功写入,以下附上对excel的操作代码:

# -*- coding: utf-8 -*-

this program is used to operate the the document, such as excel and text
this program i the assignment of the python learning 

import xlrd
import xlwt
from datetime import date,datetime
from xlutils.copy import copy

def read_excel(name,user_name):     #输入文件名和用户名
    # 打开文件
    workbook = xlrd.open_workbook(name)
    # 获取所有sheet
    #print (workbook.sheet_names()) # ['sheet1']
    sheet1_name = workbook.sheet_names()[0]

    # 根据sheet索引或者名称获取sheet内容,read the content of the sheet
    sheet1 = workbook.sheet_by_index(0) # sheet索引从0开始
    sheet1 = workbook.sheet_by_name('sheet1')  #通过名称来找到sheet2

    # sheet的名称,行数,列数
    print (sheet1.name,sheet1.nrows,sheet1.ncols)

    # 获取整行和整列的值,数组.
    rows = sheet1.row_values(0) # 获取第0行内容
    cols = sheet1.col_values(2) # 获取第3列内容
    #print ((rows))
    #print (cols)

    # 获取单元格内容
    #print (sheet1.cell(1,1).value)
    #print (sheet1.cell_value(1,1))
    #print (sheet1.row(1)[1].value)
    
    # 获取单元格内容的数据类型
    #print (sheet1.cell(0,1).ctype)
    for i in range(sheet1.nrows):
    	if i < sheet1.nrows:
    		if sheet1.cell(i,0).value == user_name:
    			game_times = sheet1.cell(i,1).value
    			min_times = sheet1.cell(i,2).value
    			total_times = sheet1.cell(i,3).value
    			have_user = 1
    			break
    		else:
    			have_user = 0
    			game_times = 0
    			min_times = 0
    			total_times = 0
    if have_user == 0:
    	wb = copy(workbook)
    	ws = wb.get_sheet(0)
    	style = xlwt.easyxf('font:height 240, color-index red, bold on;align: wrap on, vert centre, horiz center')
    	ws.write(sheet1.nrows, 0, user_name, style)
    	ws.write(sheet1.nrows, 1, str(game_times), style)
    	ws.write(sheet1.nrows, 2, str(min_times), style)
    	ws.write(sheet1.nrows, 3, str(total_times), style)
    	wb.save(r'new_excel.xlsx')
    return have_user,game_times,min_times,total_times




#save the game result to the docunments
def save_geme(game_times,min_times,total_times,times):       #"times" are this round of games' times, and this function needs this(times) to update the old record
	if game_times == 0 or times < min_times:
		min_times = times
	total_times = total_times + times
	game_times += 1
	#write this record to the document
	sheet1 = read_excel('game.xlsx')
	sheet1.cell(1,0).value = game_times
	sheet1.cell(1,1).value = min_times
	sheet1.cell(1,2).value = total_times
	#sheet1 = workbook.sheet_by_name('sheet1')
	#workbook.write('A1',game_times)
	#workbook.write('B1',min_times)
	#workbook.write('C1',total_times)               #暂时还没考虑是否需要关闭文件,最好做一个输出
	print(sheet1.cell(0,0).value,":",sheet1.cell(1,0).value)
	print(sheet1.cell(0,1).value,":",sheet1.cell(1,1).value)
	print(sheet1.cell(0,2).value,":",sheet1.cell(1,2).value)

if __name__ == '__main__':
	
	# start the real game
	print("请输入您的游戏用户名:")
	user_name = input()
	[have_user,game_times,min_times,total_times] = read_excel('game.xlsx',user_name)    #read the xlsx file
	if int(game_times) != 0:
		print(type(total_times))
		avg_times = float(total_times) / int(game_times)	
	else:
		avg_times = 0
	print('{0}已经玩了{1}次,最少是{2}轮猜出答案,平均{3}轮猜出答案'.format(user_name,int(game_times),int(min_times),total_times))

	#start this round game
	times = 0          #this round game' times

运行后:这里写图片描述
就会多出一个.xlsx文件,改成.xls文件就可以啦。并且写入也成功了:这里写图片描述

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值