先判断是否安装了xlrd库:pip install xlrd
import xlrd # 给出excel文件绝对路径 loc = ("C:\统计.xlsx") # 打开工作表 wb = xlrd.open_workbook(loc) # 这里读取的是第一个sheet sheet = wb.sheet_by_index(0) #print(sheet.cell_value(2, 0) ) #print("行数:") #print(sheet.nrows) #print("列数:") #print(sheet.ncols) for i in range(sheet.ncols): print(sheet.cell_value(3, i))
另一个库是:pip install xlwt
import xlwt from xlwt import Workbook # 创建workbook wb = Workbook() # 使用add_sheet函数创建新的sheet sheet1 = wb.add_sheet('Sheet 1') # 写入数据,参数分别为行、列、数据 sheet1.write(1, 0, 'ISBT DEHRADUN') sheet1.write(2, 0, 'SHASTRADHARA') sheet1.write(3, 0, 'CLEMEN TOWN') sheet1.write(4, 0, 'RAJPUR ROAD') sheet1.write(5, 0, 'CLOCK TOWER') sheet1.write(0, 1, 'ISBT DEHRADUN') sheet1.write(0, 2, 'SHASTRADHARA') sheet1.write(0, 3, 'CLEMEN TOWN') sheet1.write(0, 4, 'RAJPUR ROAD') sheet1.write(0, 5, 'CLOCK TOWER') sheet1.write(0, 6, '合計する') # 保存到excel表格 wb.save('C:\Users\E2-Li.y\Desktop\Java统计.xls')
加粗:
# 创建workbook workbook = xlwt.Workbook() # 创建sheet sheet = workbook.add_sheet("Sheet Name") # 给单元格内容添加格式:加粗、标红 style = xlwt.easyxf('font: bold 1,color: red') # 在单元格中写入数据 sheet.write(0, 0, 'SAMPLE', style) # 保存excel workbook.save("sample.xls")