python之自动生成器(持续更新)

Python之文章生成器
Python之文章生成器(升级版,也就是更傻瓜式运行)
狗屁不通文章生成器
Python之手把手教你自动获取青年大学习完成图片,并生成朋友圈截图

1、小学生计算题自动生成器

源码:

  1 import random, os
  2 import PySimpleGUI as sg
  3 from docx import Document
  4 from docx.shared import RGBColor, Pt, Mm, Inches
  5 from docx.enum.text import WD_ALIGN_PARAGRAPH
  6 from docx.oxml.ns import qn
  7 
  8 '''
  9 该程序产生口算题doc文件:
 10 
 11 '''
 12 document = Document()
 13 
 14 '''
 15 生成随机数,默认产生2位数的随机数
 16 '''
 17 
 18 
 19 def randomtoNUM(bit=2):
 20     if bit == 2:
 21         return random.randint(10, 99)
 22     elif bit == 3:
 23         return random.randint(100, 999)
 24     elif bit == 1:
 25         return random.randint(1, 9)
 26 
 27 
 28 '''
 29 2位数减法(含退位)
 30 '''
 31 
 32 
 33 def chutijian():
 34     jia1_10 = random.randint(1, 9)
 35     jia1_1 = random.randint(0, 8)
 36     jia1 = int(str(jia1_10) + str(jia1_1))
 37     # print(8-int(str(jia1)[0]))
 38     jia2_max10 = int(str(jia1)[0]) - 1
 39     if jia2_max10 < 1:
 40         jia2_10 = jia2_max10
 41     else:
 42         jia2_10 = random.randint(0, jia2_max10)
 43 
 44     if int(str(jia1)[1]) + 1 >= 9:
 45         jia2_1 = 9
 46     else:
 47         jia2_1 = random.randint(int(str(jia1)[1]) + 1, 9)
 48     jia2 = int(str(jia2_10) + str(jia2_1))
 49     # print(eval("{} + {}".format(jia1,jia2)))
 50 
 51     return "{:<2d} - {:>2d}".format(jia1, jia2)
 52 
 53 
 54 '''
 55 2位数加法(含进位)
 56 '''
 57 
 58 
 59 def chutiAdd():
 60     jia1_10 = random.randint(1, 8)
 61     jia1_1 = randomtoNUM(1)
 62     jia1 = int(str(jia1_10) + str(jia1_1))
 63     # print(8-int(str(jia1)[0]))
 64     jia2_max10 = 8 - int(str(jia1)[0])
 65     if jia2_max10 <= 1:
 66         jia2_10 = abs(jia2_max10)
 67     else:
 68         jia2_10 = random.randint(1, jia2_max10)
 69 
 70     if 10 - int(str(jia1)[1]) >= 9:
 71         jia2_1 = 9
 72     else:
 73         jia2_1 = random.randint(10 - int(str(jia1)[1]), 9)
 74     jia2 = int(str(jia2_10) + str(jia2_1))
 75     # print(eval("{} + {}".format(jia1,jia2)))
 76     if random.randint(0, 1):
 77         return "{:<2d} + {:>2d}".format(jia1, jia2)
 78     else:
 79         return "{:<2d} + {:>2d}".format(jia2, jia1)
 80 
 81 
 82 '''
 83 1位数加法(含进位)
 84 '''
 85 
 86 
 87 def chuti1BitAddH():
 88     jia1 = randomtoNUM(1)
 89     jia2 = 10 - jia1  # 第二个数最小值
 90 
 91     if jia2 == 1:
 92         jia2 = 9
 93     else:
 94         jia2 = random.randint(10 - jia1, 9)
 95 
 96     if random.randint(0, 1):
 97         return "{:<2d} + {:>2d}".format(jia1, jia2)
 98     else:
 99         return "{:<2d} + {:>2d}".format(jia2, jia1)
100 
101 
102 '''
103 1位数加法(不含进位)
104 '''
105 
106 
107 def chuti1BitAdd():
108     jia1 = random.randint(1, 8)
109     jia2 = 9 - jia1  # 第二个数最大值
110 
111     if jia2 <= 1:
112         jia2 = 1
113     else:
114         jia2 = random.randint(1, jia2)
115 
116     if random.randint(0, 1):
117         return "{:<2d} + {:>2d}".format(jia1, jia2)
118     else:
119         return "{:<2d} + {:>2d}".format(jia2, jia1)
120 
121 
122 '''
123 1位数减法
124 '''
125 
126 
127 def chuti1Bitsub():
128     jian1 = random.randint(2, 9)
129     jian2 = jian1 - 1
130 
131     if jian2 <= 1:
132         jian2 = 1
133     else:
134         jian2 = random.randint(1, jian2)
135 
136     return "{:<2d} - {:>2d}".format(jian1, jian2)
137 
138 '''
139 产生2个相减需退位的数,即第1个数小于第二个数
140 '''
141 def gtTenSub():
142     jia1 = random.randint(0, 8)
143     jia2 = jia1+1
144 
145     if jia2 >= 9:
146         jia2 = 9
147     else:
148         jia2 = random.randint(jia2, 9)
149 
150     return jia1, jia2
151 
152 '''
153 产生2个相减不退位的数,第1个数大于第二个数
154 '''
155 
156 
157 def ltTenSub():
158     jia1 = random.randint(2, 9)
159     jia2 = jia1-1  # 第二个数最大值
160 
161     if jia2 <= 1:
162         jia2 = 1
163     else:
164         jia2 = random.randint(1, jia2)
165     return jia1, jia2
166 
167 
168 
169 '''
170 产生2个相加进位的数
171 '''
172 
173 
174 def gtTen():
175     jia1 = random.randint(1, 9)
176     jia2 = 9 - jia1
177 
178     if jia2 >= 9:
179         jia2 = 9
180     else:
181         jia2 = random.randint(jia2 + 1, 9)
182 
183     return jia1, jia2
184 
185 
186 '''
187 产生2个相加不进位的数
188 '''
189 
190 
191 def ltTen():
192     jia1 = random.randint(1, 8)
193     jia2 = 9 - jia1  # 第二个数最大值
194 
195     if jia2 <= 1:
196         jia2 = 1
197     else:
198         jia2 = random.randint(1, jia2)
199     return jia1, jia2
200 
201 
202 '''
203 3位数相加
204 
205 '''
206 
207 
208 def chuti3BitAdd():
209     listone = []
210     listtwo = []
211     gttenNum = random.randint(1, 3)  # 选出需要进位的个数
212     for i in range(gttenNum):
213         one, two = gtTen()
214         listone.append(str(one))
215         listtwo.append(str(two))
216 
217     for j in range(3 - gttenNum):
218         one, two = ltTen()
219         listone.append(str(one))
220         listtwo.append(str(two))
221     #print('完整:', ''.join(listone), ''.join(listtwo))
222     if random.randint(0, 1):
223         return "{:>8s} \n+ {:>5s}\n ̄ ̄ ̄ ̄ ̄\n\n".format(''.join(listone), ''.join(listtwo))
224     else:
225         return "{:>8s} \n+ {:>5s}\n ̄ ̄ ̄ ̄ ̄\n\n".format(''.join(listtwo), ''.join(listone))
226 
227 
228 
229 '''
230 3位数相减
231 
232 '''
233 
234 
235 def chuti3BitSub():
236     listone = []
237     listtwo = []
238     gttenNum = random.randint(1, 2)  # 选出需要退位的个数
239     for i in range(gttenNum):
240         one, two = gtTenSub()
241         listone.append(str(one))
242         listtwo.append(str(two))
243 
244     for j in range(3 - gttenNum):
245         one, two = ltTenSub()
246         listone.append(str(one))
247         listtwo.append(str(two))
248     #print('完整:', ''.join(listone), ''.join(listtwo))
249     listone.reverse()
250     listtwo.reverse()
251     return "{:>8s} \n- {:>5d}\n ̄ ̄ ̄ ̄ ̄\n\n".format(''.join(listone), int(''.join(listtwo)))
252 
253 
254 '''
255 
256 整十或整百加减
257 '''
258 
259 
260 def allplugsub():
261     localhandleFunctionlist = ['chuti1BitAdd', 'chuti1BitAddH', 'chuti1Bitsub', 'chutiAdd', 'chutijian']
262     resulttemp = eval(random.choice(localhandleFunctionlist) + "()")
263     strarr = resulttemp.split('')
264     isFlagplugsub = len(strarr)  # 是1为加法,2为减法
265     beishu = random.choice(['0', '00'])
266     if isFlagplugsub == 2:  # 减法
267         jian1 = strarr[0].strip() + beishu
268         jian2 = strarr[1].strip() + beishu
269         return "{:<2d} - {:>2d}".format(int(jian1), int(jian2))
270     else:  # 加法
271         strarr = resulttemp.split('')
272         jia1 = strarr[0].strip() + beishu
273         jia2 = strarr[1].strip() + beishu
274         if random.randint(0, 1):
275             return "{:<2d} + {:>2d}".format(int(jia1), int(jia2))
276         else:
277             return "{:<2d} + {:>2d}".format(int(jia2), int(jia1))
278 
279 
280 '''
281 产生doc文档
282 '''
283 
284 
285 def makedoc(handlelist,pagenum):
286     '''
287     p = document.add_paragraph()
288 
289     p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
290     run = p.add_run(u'小学生计算题练习 ')
291     run.font.color.rgb = RGBColor(0, 0, 0)
292     run.font.size = Pt(28)
293     p.space_before = Pt(40)
294 
295     p.add_run().add_break(break_type=6)
296     '''
297 
298     document.styles['Normal'].font.name = u'宋体'
299     document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
300     document.styles['Normal'].font.size = Pt(16)
301 
302     ti = document.add_paragraph()
303     ti.alignment = WD_ALIGN_PARAGRAPH.LEFT
304     for nump in range(pagenum):
305         tishu = 45
306         if len(handlelist) ==1 and ('chuti3BitAdd' in handlelist or 'chuti3BitSub' in handlelist):
307             tishu = 21
308         if len(handlelist) ==2 and ('chuti3BitAdd' in handlelist and 'chuti3BitSub' in handlelist):
309             tishu = 21
310 
311         for i in range(tishu):  # 产生45题口算题
312 
313             # 根据函数名称随机选择题目类型,handlelist list的值,可用的函数'chutiAdd', 'chutijian','chuti1BitAddH','chuti1BitAdd'
314             if tishu == 45:
315                 content = u"{:<7}=".format(eval(random.choice(handlelist) + "()"))
316                 run1 = ti.add_run("{:<40}".format(content))
317             elif tishu == 21:
318                 content = u"{}".format(eval(random.choice(handlelist) + "()"))
319                 if i==20 and nump==pagenum-1:
320                     content=content.strip('\n\n')
321 
322                 run1 = ti.add_run("{}".format(content))
323             run1.font.color.rgb = RGBColor(0, 0, 0)
324             run1.font.size = Pt(16)
325             if tishu == 45:
326                 ti.paragraph_format.line_spacing = Pt(45)
327 
328     #print(len(document.sections))
329     for section in document.sections:
330         header = section.header
331         paragraph = header.paragraphs[0]
332         paragraph.text = '                     小学生计算题练习                  '
333         paragraph.style = document.styles["Header"]
334 
335         section.top_margin = Inches(0.7)
336         section.bottom_margin = Inches(0.7)
337         section.left_margin = Inches(0.7)
338         section.right_margin = Inches(0.7)
339 
340         # Columns = 1
341         sectPr = section._sectPr
342         cols = sectPr.xpath('./w:cols')[0]
343         cols.set(qn('w:num'), '3')
344 
345     document.save('test.docx')  # 可以设置其他路径
346 
347     os.startfile("test.docx")
348 
349 
350 # ff     GreenTan
351 sg.ChangeLookAndFeel('LightGreen')
352 ok_btn = sg.SimpleButton('生成习题', size=(10, 2), font=("微软雅黑", 12), button_color=('white', 'firebrick3'))
353 cancel_btn = sg.Button('关闭程序', size=(10, 2), font=("微软雅黑", 12))
354 layout = [
355     [sg.Checkbox('1位数加,不进位', default=False, size=(35, 2), font=("微软雅黑", 12))],
356     [sg.Checkbox('1位数加,进位', size=(35, 2), font=("微软雅黑", 12))],
357     [sg.Checkbox('1位数减', size=(35, 2), font=("微软雅黑", 12))],
358     [sg.Checkbox('2位数加,进位', size=(35, 2), font=("微软雅黑", 12))],
359     [sg.Checkbox('2位数减,退位', size=(35, 2), font=("微软雅黑", 12))],
360     [sg.Checkbox('整十或整百加减', size=(35, 2), font=("微软雅黑", 12))],
361     [sg.Checkbox('3位数相加,进位,只能与竖式同时选择', size=(35, 2),text_color=('blue'), font=("微软雅黑", 12))],
362     [sg.Checkbox('3位数减法,退位,只能与竖式同时选择', size=(35, 2),text_color=('blue'), font=("微软雅黑", 12))],
363    # [sg.Slider(range=(1,5), orientation='h',size=(35, 10),  font=("微软雅黑", 12))],
364     #[sg.Text('This is some text', font='Courier 12', text_color='blue', background_color='green')],
365     #[sg.Spin([1,2,3,4,5], size=(35, 10),  font=("微软雅黑", 12))],
366 [sg.Text('要生成几页:',auto_size_text=True,size=(15, 1), font=("微软雅黑", 12))],
367 [sg.Slider(range=(1,6), orientation='h',size=(35, 15),  font=("微软雅黑", 12))],
368     [ok_btn, cancel_btn],[sg.StatusBar('好好学习,天天向上\n编写语言:python ',size=(400,10), font=("微软雅黑", 12))]
369 
370 ]
371 
372 window = sg.Window('小学生计算题生成器', default_element_size=(40, 2), size=(400, 485)).Layout(layout)
373 
374 handleFunctionlist = ['chuti1BitAdd', 'chuti1BitAddH', 'chuti1Bitsub', 'chutiAdd', 'chutijian', 'allplugsub',
375                       'chuti3BitAdd','chuti3BitSub']
376 while True:
377     event, values = window.read()
378     if event in (None, '关闭程序'):
379         # User closed the Window or hit the Cancel button
380         break
381     elif event in (None, '生成习题'):
382         # sg.Popup(event, values)
383         handlelist = []
384         #print(values)
385 
386         for i in values:
387 
388             if type(values[i])==bool and values[i]:
389                 handlelist.append(handleFunctionlist[i])
390             #print(handlelist)
391         pagenum=int(values[8])
392         #print(pagenum)
393         makedoc(handlelist,pagenum)
394 
395         break
396 
397 window.close()

 

运行界面,选好后点击生成习题:

在这里插入图片描述

生成的文件

在这里插入图片描述

生成的习题,可打印

在这里插入图片描述

运行时报错 :ModuleNotFoundError: No module named ‘exceptions’

解决方法:Python运行时报错 ModuleNotFoundError: No module named ‘exceptions’

2、自动生成考生信息

name_list 姓名库

在这里插入图片描述

school_list 学校库

在这里插入图片描述

function.py 各型函数汇总

 1 import random
 2 
 3 class Student():
 4     #初始化
 5     def __init__(self):
 6         self.__name=''      #姓名
 7         self.__city=''      #城市
 8         self.__school=''    #学校
 9         self.__height=0     #身高
10         self.__weight=0     #体重
11         self.__breast=''    #罩杯
12         self.__score=0      #高考分数
13         self.__subject=''   #选修科目
14         self.__level=''     #选修等第
15     #设立身高体重
16     def build_body(self):
17         self.__height=random.randint(158,176)
18         temp_height=self.__height/100
19         BMI=random.uniform(18,21)
20         self.__weight=int(BMI*temp_height*temp_height)
21         flag1=random.randint(1,20)
22         if 158<=self.__height<=163:
23             if 1<=flag1<=14:self.__breast='A'
24             else:self.__breast='B'
25         elif 164<=self.__height<=170:
26             if 1<=flag1<=8:self.__breast='A'
27             elif 9<=flag1<=17:self.__breast='B'
28             else:self.__breast='C'
29         else:
30             if 1<=flag1<=3:self.__breast='A'
31             elif 4<=flag1<=12:self.__breast='B'
32             elif 13<=flag1<=18:self.__breast='C'
33             else:self.__breast='D'
34         
35     #随机设立成绩    
36     def create_score(self):
37         self.__score=random.randint(301,400)
38 
39     #设立必修等级
40     def create_level(self):
41         subject=['政史','史地','物化','物生','物地']
42         level=['C','C','B','B+','A','A+','A+']
43         self.__subject=subject[random.randint(0,4)]
44         
45         flag1=0
46         if 300<self.__score<=320:flag1=1
47         elif 320<self.__score<=340:flag1=2
48         elif 340<self.__score<=360:flag1=3
49         elif 360<self.__score<=380:flag1=4
50         else:flag1=5
51         flag2=flag1+random.randint(-1,0)
52         self.__level=level[flag1]+level[flag2]
53 
54     #设立姓名
55     def set_name(self,name):
56         self.__name=name
57 
58     #设立学校
59     def set_school(self):
60         city_list=[]
61         school_list=[]
62         with open(r'school_list\readme.txt') as file1:
63             for city in file1:
64                 city_list.append(city.strip())
65         flag1=random.randint(0,12)
66         self.__city=city_list[flag1]
67         file_name=r"school_list"+ "\\" +str(flag1+1)+'.txt'
68         
69         with open(file_name) as file2:
70             for school in file2:
71                 school_list.append(school.strip())
72         flag2=random.randint(0,len(school_list)-1)
73         self.__school=school_list[flag2]
74             
75     #打印结果
76     def show(self):
77         inf='姓名:' +self.__name
78         inf+='  身高:'+str(self.__height)+'cm'
79         inf+='  体重:'+str(self.__weight)+'kg'
80         inf+='  罩杯:'+self.__breast
81         inf+='  城市:'+self.__city
82         inf+='  学校:'+self.__school
83         inf+='  高考成绩:'+str(self.__score)
84         inf+='  选科:'+self.__subject
85         inf+='  等第:'+self.__level
86         return inf

 

main.py 主程序

 1 from function import Student
 2 import random
 3 
 4 #名字初始化
 5 xing=[]
 6 ming=[]
 7 with open(r"name_list\xingshi.txt") as file1:
 8     for line in file1:
 9         xing.append(line.strip())
10 lx=len(xing)
11 with open(r"name_list\mingzi.txt") as file2:
12     for line in file2:
13         ming.append(line.strip())
14 lm=len(ming)
15 #循环(暂时500个)
16 num=input("输入你想生成的学生信息数量:")
17 with open("inf.txt",'a') as file3:
18     for i in range(int(num)):
19         t=Student()
20         #名字
21         flag1=random.randint(1,lx)-1
22         flag2=random.randint(1,lm)-1
23         t.set_name(xing[flag1]+ming[flag2])
24         #其他
25         t.build_body()
26         t.create_score()
27         t.create_level()
28         t.set_school()
29         inf=t.show()
30         file3.write(inf+'\n')
31 print("已生成!")

 

运行效果:

输入数量,自动生成到主目录的inf.txt
在这里插入图片描述
打开inf.txt:
在这里插入图片描述

3、自动生成新年祝福

代码

 1 import random
 2 
 3 def generateWish1():
 4     list1=['椒花献颂,','春回柳叶,','天开淑景,','地暖春风,','天高地阔,','风光胜旧,','岁序更新,','天翔紫燕,','喜鹊鸣春,','花香四季,','月满一轮,','花迎春光,','牛耕绿野,','江山秀丽,','虎啸青山,','无边春舍,',
 5     '有福人家,','龙吟国瑞,','虎啸年丰,','龙兴华夏,','百花献瑞,','百花齐放,','岁且更始,','时乃日新,','莺歌燕舞,','春光骀荡,','万物回春,','国光蔚起,','民气昭苏,','国步龙腾,','天开化宇,']
 6     s=random.choice(list1)
 7     list1.remove(s)#删除已被选择的元素,以免重复,下同
 8     return s
 9     #这个列表是套话
10 
11 def generateWish2():
12     list2=['学业有成,','福星高照,','万事如意,','日月皆春,','江山永固,','福寿安康,','岁岁平安,','年年有余,','腰缠万贯,','财源亨通,','金玉满堂,','喜气洋洋,','万事如意,','大吉大利,','三阳开泰,','财源广进,',
13     '四时喜庆,','五谷丰登,','四时如意,','万事遂心,','燕舞新春,','平安无恙,','吉庆有余,','福星高照,','恭贺新春,','吉庆有余,','福享新春,','喜气盈门,','三阳开泰,','励精图治,','革故鼎新,','抬头见喜,','吉星高照,','恭喜发财,','心想事成,','五福临门,','五谷丰登,','门凝瑞霭,','户发春光,',
14     '人寿年丰,','人乐丰年,','一门瑞气,','万里春风,','千祥云集,','百福骈臻,','人登寿域,','世跻春台,','万马奔腾,','满院春光,','庆云跃日,','金玉满堂,','龙凤呈祥,','百业兴旺,','吉星高照,','吉祥如意,','开春大吉,','五谷丰登,','万事顺利,','万事顺意,','六蓄兴旺,','荣华富贵,','金玉满堂,',
15     '鹏程万里,','笑逐颜开,','心旷神怡,','财源广进,','阖家欢乐,','飞黄腾达,','万事顺意,','幸福美满,','官运亨通,','美梦连连,','万事顺利,','龙凤呈祥,','红红火火,','二龙腾飞,','三阳开泰,','四季平安,','五福临门,','六六大顺,','七星高照,','八方来财,','九九同心,','一帆风顺,']
16     s=random.choice(list2)
17     list2.remove(s)
18     return s
19     #这个列表是各种祝福语
20 
21 if __name__ == '__main__':
22     name=input("Please input the person's name:")
23     string='牛年新春将至,'
24     i=0
25     for i in range (0,3):#这里默认选择3个成语,可自行调整
26         string+=generateWish1()
27         i+=1
28     string1=list(string)#字符串转列表
29     string1[-1]='' #将此部分的逗号转换为句号,使句意通顺,下同
30     string=''.join(string1) #列表转回字符串
31     s1=name+',在新的一年里,祝你'
32     string+=s1
33     for i in range (0,5):
34         string+=generateWish2()
35         i+=1
36     string1=list(string) #字符串转列表
37     string1[-1]='!'
38     string=''.join(string1)
39     print(string)

 

运行效果:

在这里插入图片描述
在这里插入图片描述

 

posted @ 2021-02-12 17:28  BugMiaowu2021  阅读(599)  评论(0)    收藏  举报