import xlrd,xlwt #打开excel文件并获取所有sheet workbook = xlrd.open_workbook(r'D:\1.xlsx') sheetlist=workbook.sheet_names() # print (sheetlist) coldata=[] for ele in sheetlist: sheet=workbook.sheet_by_name(ele) col=sheet.ncols row=sheet.nrows # print("打印"+ele) # for i in range(col): # print("第"+str(i)+"列") # print(sheet.col_values(i)) # print("====================================") for j in range(col): # print("第"+str(j)+"行列") # print(sheet.row_values(j)) coldata.append(sheet.col_values(j)) import xlsxwriter print(coldata) #创建一个新的xlsx文件(如果原有同名文件会被覆盖) workbook = xlsxwriter.Workbook("test01.xlsx") #创建一个新的表单,默认名称为 “sheet1”,输入字符参数可指定名称 worksheet = workbook.add_worksheet() row = 0 col = 0 #worksheet.write 方法将数据写入 xlsx 表格中 # #参数依次为:行号、列号、数据、[格式] # for item, cost in (expenses): # worksheet.write(row, col , item) # worksheet.write(row, col + 1, cost) # row += 1 # print(item) # print(cost) bold=workbook.add_format({'bold':2}) headers=["姓名","性别","年龄"] colname=["A2","B2","C2","D2","E2","F2","G2","H2","I2","J2","K2","L2","M2","N2","O2","P2","Q2"]#定义一个列名 worksheet.write_row('A1',headers) for i in range(0,len(coldata)): worksheet.write_column(colname[i],coldata[i]) print(colname[i],coldata[i]) #合并单元格 worksheet.merge_range('A1:C2',"cesdfd") #显式关闭workbook,若不显式指定,则作用域结束后自动关闭 workbook.close()