今日進行了利用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文件就可以啦。並且寫入也成功了:。