1 Star 0 Fork 0

王晓光/GeneratePrintWordPaper

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.py 3.73 KB
一键复制 编辑 原始数据 按行查看 历史
王晓光 提交于 2021-11-03 16:35 . Execl打印文件生成
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)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/DawnLights/generate-print-word-paper.git
git@gitee.com:DawnLights/generate-print-word-paper.git
DawnLights
generate-print-word-paper
GeneratePrintWordPaper
master

搜索帮助