python 在Excel 表格中生成 九九乘法表

生成效果如下的 九九乘法表

 

import os, openpyxl
from openpyxl.styles import Font

#path = 'D:\pyspace'
#os.chdir(path)

wb = openpyxl.Workbook()
sheet = wb.active

#从 B2 单元格开始向左向下各填充 1~9,设置加粗字体
fontB = Font(b=True)
for i in range(3, 12):
    cell2 = sheet.cell(row=2, column=i)
    cell2.value = i - 2
    cell2.font = fontB
    cellB = sheet.cell(row=i, column=2)
    cellB.value = i - 2
    cellB.font = fontB

#根据条件生成 99 乘法表
for i in range(3, 12):
    for j in range(3, 12):
        x = sheet.cell(row=2, column=j).value
        y = sheet['B' + str(i)].value
        if x <= y:
            sheet.cell(row=i, column=j).value = x*y
#保存文件
wb.save('九九乘法表.xlsx')

 

原文地址:https://www.cnblogs.com/shanger/p/12925236.html