代码拉取完成,页面将自动刷新
import datetime
import math
import random
import xlsxwriter
import sqlite_crud as sq
def list_of_groups(init_list, children_list_len):
list_of_groups = zip(*(iter(init_list),) * children_list_len)
end_list = [list(i) for i in list_of_groups]
count = len(init_list) % children_list_len
end_list.append(init_list[-count:]) if count != 0 else end_list
return end_list
def word_list_to_execl(excel_word_list):
# 文件名随机
execl_file_name = "PrintWord_" + datetime.datetime.now().strftime('%Y%m%d_%f') + ".xlsx"
# 创建Execl文件和Sheet.
workbook = xlsxwriter.Workbook(execl_file_name)
worksheet = workbook.add_worksheet("WordTest")
# 将页面方向设置为纵向
worksheet.set_portrait()
# 设置纸张类型(A4)
worksheet.set_paper(9)
# 设置边距
worksheet.set_margins(left=0.3, right=0.3, top=0.3, bottom=0.3)
# 设置初始行高
worksheet.set_default_row(24)
# 设置列宽
worksheet.set_column(0, 0, 36)
worksheet.set_column(1, 1, 10)
worksheet.set_column(2, 2, 2)
worksheet.set_column(3, 3, 36)
worksheet.set_column(4, 4, 10)
# 准备样式
borderFormat = workbook.add_format({'bottom': 1})
temp_word_num = len(excel_word_list)
temp_word_num_fenzu = math.ceil(temp_word_num/2)
# print("temp_word_num_fenzu=" + str(temp_word_num_fenzu))
excel_word_list = list_of_groups(excel_word_list, temp_word_num_fenzu)
# print(excel_word_list[0])
# print(excel_word_list[1])
row = 0
col = 0
for word_line in excel_word_list[0]:
worksheet.write(row, col, word_line[1], borderFormat)
worksheet.write(row, col+1, "", borderFormat)
row += 1
row1 = 0
col1 = 3
for word_line2 in excel_word_list[1]:
worksheet.write(row1, col1, word_line2[1], borderFormat)
worksheet.write(row1, col1+1, "", borderFormat)
row1 += 1
workbook.close()
print("生成文件:" + execl_file_name)
if __name__ == '__main__':
# 数据准备
no_book = ""
no_lesson_from = ""
no_lesson_to = ""
all_or_part = ""
no_export_execl = ""
while not no_book.isdigit():
no_book = input("请输入新概念英语第几册(默认是1):")
if no_book.strip() == "":
no_book = "1"
while not no_lesson_from.isdigit():
no_lesson_from = input("请输入新概念英语从第几课开始(默认是1):")
if no_lesson_from.strip() == "":
no_lesson_from = "1"
while not no_lesson_to.isdigit():
no_lesson_to = input("请输入新概念英语到第几课结束:")
while not all_or_part.isdigit():
all_or_part = input("默写全部还是部分(输入数字,0代表全部):")
print()
print("新概念英语第" + no_book + "册[Lesson " + no_lesson_from + "-" + no_lesson_to + "]单词准备中")
# 数据准备
word_list = sq.sql_select_new_concept(no_book, no_lesson_from, no_lesson_to)
word_num = len(word_list)
print("单词总数:" + str(word_num))
if all_or_part == "0":
print("默写总数:" + str(word_num))
random.shuffle(word_list)
word_list_to_execl(word_list)
if all_or_part != "0" and all_or_part.isdigit():
print("默写总数:" + all_or_part)
if len(all_or_part) > len(word_list):
print("Error:默写总数超过单词总数")
else:
while not no_export_execl.isdigit():
no_export_execl = input("需要几份(默认是1):")
if no_export_execl.strip() == "":
no_export_execl = "1"
for i in range(int(no_export_execl)):
random_list = random.sample(word_list, int(all_or_part))
word_list_to_execl(random_list)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。