from __future__ import print_function
import random
import xlwt
# 可以输出带字母的舒尔特表,设置is_number=False即可
is_number=True
def schulte(n):
'''return n*n'''
# 打乱列表
max = n * n
if is_number:
numbers = list(range(1, max + 1)) # 兼容py3
else:
digits = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',1,2,3,4,5,6,7,8,9,10,11,12]
numbers = digits[:max]
random.shuffle(numbers)
# 输出到excel
# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding='utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('sheet1')
# 为样式创建字体
font = xlwt.Font()
# 字体类型
font.name = '华文仿宋'
# 字体颜色
# font.colour_index = i
# 字体大小,11为字号,20为衡量单位
font.height = 20 * 36
# 字体加粗
font.bold = False
# 下划线
font.underline = False
# 斜体字
font.italic = False
# 设置单元格对齐方式
alignment = xlwt.Alignment()
# 0x01(左端对齐)、0x02(水平方向上居中对齐)、0x03(右端对齐)
alignment.horz = 0x02
# 0x00(上端对齐)、 0x01(垂直方向上居中对齐)、0x02(底端对齐)
alignment.vert = 0x01
# 设置自动换行
alignment.wrap = 1
# 设置边框
borders = xlwt.Borders()
# 细实线:1,小粗实线:2,细虚线:3,中细虚线:4,大粗实线:5,双线:6,细点虚线:7
# 大粗虚线:8,细点划线:9,粗点划线:10,细双点划线:11,粗双点划线:12,斜点划线:13
borders.left = 1
borders.right = 1
borders.top = 1
borders.bottom = 1
# borders.left_colour = i
# borders.right_colour = i
# borders.top_colour = i
# borders.bottom_colour = i
# 设置列宽,一个中文等于两个英文等于两个字符,11为字符数,256为衡量单位
# worksheet.col(1).width = 3 * 256
# 设置背景颜色
# pattern = xlwt.Pattern()
# 设置背景颜色的模式
# pattern.pattern = xlwt.Pattern.SOLID_PATTERN
# 背景颜色
# pattern.pattern_fore_colour = 5
# 初始化样式
style0 = xlwt.XFStyle()
style0.font = font
# style0.pattern = pattern
style0.alignment = alignment
style0.borders = borders
# 设置文字模式
# font.num_format_str = '#,##0.00'
# 格式化输出
print()
i = 0
row = 0
while i < max:
for index,x in enumerate(numbers[i: i + n]):
print(x, '\t', end='')
# 写入excel
# 参数对应 行, 列, 值
worksheet.write(row, index, x,style0)
print('\r')
i += n
row += 1
# 保存
workbook.save('shuerte.xls')
schulte(8)