1 import openpyxl 2 from openpyxl.styles import Font 3 4 wb = openpyxl.Workbook() 5 sheet = wb.get_active_sheet() 6 7 font = Font(size=20,bold=True) # 粗体 8 9 # 生成乘法表 10 num = int(input('请输入最大数字:')) 11 nums_row = list(range(1, num+1)) 12 nums_column = list(range(1, num+1)) 13 nums_result = [] 14 15 for num_row in nums_row: 16 def f(x): 17 return num_row * x 18 nums_result.append(list(map(f, nums_column))) # map(f(), nums_column)报错 19 20 # 设置表头格式,添加表头内容 21 for i in range(2, num+2): 22 sheet.cell(row=1, column=i).font = font 23 sheet.cell(row=1, column=i).value = nums_column[i-2] 24 sheet.cell(row=i, column=1).font = font 25 sheet.cell(row=i, column=1).value = nums_column[i-2] 26 27 # 填充乘法表 28 for i in range(2, num+2): 29 for j in range(2, num+2): 30 sheet.cell(row=i, column=j).value = nums_result[i-2][j-2] 31 32 # 保存xlsx 33 wb.save('multi_plication_table.xlsx')