import xlrd
xlrd.open_workbook(r'/root/excel/chat.xls')
# 获取所有sheet
sheet_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 xlrd
from datetime import date,datetime
arrayNum = 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 xlwt
def 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,数值等于1
mySheet.write(2, 1, 1) #写入B3,数值等于1
mySheet.write(2, 2, xlwt.Formula("A3+B3")) #写入C3,数值等于2(A3+B3)
#5. 保存
myWorkbook.save('excelFile.xls')
if __name__ == '__main__':
# 写入Excel
write_excel();
print ('写入成功')
import openpyxl
f = openpyxl.Workbook() #创建工作簿
sheet1 = f.create_sheet()
for jkey in range(len(newTables)):
jk = 1
for cT in range(arrayNum):
jk = jkey + 1
if cT == 0:
sheet1.cell(row=jk,column=cT+1).value='1'
else:
sheet1.cell(row=jk,column=cT+1).value='2'
f.save("chatPy.xlsx") #保存文件
import openpyxl
#写excel
def write_excel():
f = openpyxl.Workbook() #创建工作簿
sheet1 = f.create_sheet()
#sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) #创建sheet
row0 = [u'L1',u'L2',u'L3',u'L4',u'问题',u'答案']
#生成第一行
#for i in range(len(row0)):
# sheet1.cell(column=i,row=0).value='L1')
#生成后续
for jkey in range(len(newTables)):
jk = 1
for cT in range(arrayNum):
jk = jkey + 1
if cT == 0:
sheet1.cell(row=jk,column=cT+1).value='1'
else:
sheet1.cell(row=jk,column=cT+1).value='2'
f.save("chatPy.xlsx") #保存文件
if __name__ == '__main__':
# 写入Excel
write_excel();
print ('写入成功')
于是我找到了xlsxwriter这个模块,它生成的文件后缀名为.xlsx,最大能够支持1048576行数据,16384列数据
import xlsxwriter
workbook = xlsxwriter.Workbook('demo1.xlsx')#创建一个excel文件
worksheet = workbook.add_worksheet(u'sheet1')#在文件中创建一个名为TEST的sheet,不加名字默认为sheet1
worksheet.write(3,0,35.5)#第4行的第1列设置值为35.5
workbook.close()
import xlsxwriter
#写excel
def write_excel():
workbook = xlsxwriter.Workbook('chat.xlsx')#创建一个excel文件
worksheet = workbook.add_worksheet(u'sheet1')#在文件中创建一个名为TEST的sheet,不加名字默认为sheet1
worksheet.set_column('A:A',20)#设置第一列宽度为20像素
bold= workbook.add_format({'bold':True})#设置一个加粗的格式对象
worksheet.write('A1','HELLO')#在A1单元格写上HELLO
worksheet.write('A2','WORLD',bold)#在A2上写上WORLD,并且设置为加粗
worksheet.write('B2',U'中文测试',bold)#在B2上写上中文加粗
worksheet.write(2,0,32)#使用行列的方式写上数字32,35,5
worksheet.write(3,0,35.5)#使用行列的时候第一行起始为0,所以2,0代表着第三行的第一列,等价于A4
worksheet.write(4,0,'=SUM(A3:A4)')#写上excel公式
workbook.close()
if __name__ == '__main__':
# 写入Excel
write_excel();
print ('写入成功')
https://github.com/ShaShiDiZhuanLan/Demo_Excel_Python