Python中幾種常用包比較
import xlrd
xlrd.open_workbook(r'/root/excel/chat.xls')
# 獲取所有sheetsheet_name = workbook.sheet_names()[0]# 根據sheet索引或者名稱獲取sheet內容sheet = workbook.sheet_by_index(0) # sheet索引從0開始
sheet.cell_value(第幾行,第幾列)
# 獲取整行和整列的值(數組)rows = sheet.row_values(1) # 獲取第2行內容cols = sheet.col_values(2) # 獲取第3列內容
# sheet的名稱,行數,列數print (sheet.name,sheet.nrows,sheet.ncols)
import xlrdfrom datetime import date,datetimearrayNum = 6#array = {'L1':'','L2':'','L3':'','L4':'','Question':'','Answer':''}tables = []newTables = []def read_excel(): # 打開文件 workbook = xlrd.open_workbook(r'/root/chat.xls') # 獲取所有sheet sheet_name = workbook.sheet_names()[0] # 根據sheet索引或者名稱獲取sheet內容 sheet = workbook.sheet_by_index(0) # sheet索引從0開始 # sheet = workbook.sheet_by_name('Sheet1') #print (workboot.sheets()[0]) # sheet的名稱,行數,列數 print (sheet.name,sheet.nrows,sheet.ncols) # 獲取整行和整列的值(數組) rows = sheet.row_values(1) # 獲取第2行內容 # cols = sheet.col_values(2) # 獲取第3列內容 print (rows) # print (cols) for rown in range(sheet.nrows): array = {'L1':'','L2':'','L3':'','L4':'','Question':'','Answer':''} array['L1'] = sheet.cell_value(rown,0) array['L2'] = sheet.cell_value(rown,1) array['L3'] = sheet.cell_value(rown,2) array['L4'] = sheet.cell_value(rown,3) array['Question'] = sheet.cell_value(rown,4) array['Answer'] = sheet.cell_value(rown,5) tables.append(array) print (len(tables)) #print (tables) #print (tables[5])if __name__ == '__main__': # 讀取Excel read_excel(); print ('讀取成功')
原因
在xlwt中生成的xls文件最多能支持65536行數據。如果寫入過多,會報錯
由于數據太多,會報這個錯誤:
ValueError: row index (65536)not an intin range(65536)錯誤
源碼示例
# 1. 導入模塊import xlwtdef write_excel():# 2. 創建Excel工作薄myWorkbook = xlwt.Workbook()# 3. 添加Excel工作表mySheet = myWorkbook.add_sheet('A Test Sheet')# 4. 寫入數據myStyle = xlwt.easyxf('font: name Times New Roman, color-index red, bold on', num_format_str='#,##0.00') #數據格式mySheet.write(i, j, 1234.56, myStyle)mySheet.write(2, 0, 1) #寫入A3,數值等于1mySheet.write(2, 1, 1) #寫入B3,數值等于1mySheet.write(2, 2, xlwt.Formula("A3+B3")) #寫入C3,數值等于2(A3+B3)#5. 保存myWorkbook.save('excelFile.xls')if __name__ == '__main__': # 寫入Excel write_excel(); print ('寫入成功')
新聞熱點
疑難解答