#_*_ coding:utf-8 _*_
importrandom,os,pymysqlfrom docx importDocumentfrom docx.shared importInches,Ptfrom docx.enum.text importWD_ALIGN_PARAGRAPH,WD_LINE_SPACINGfrom docx.oxml.ns importqnfrom docx.shared importInchesclassSunckSql():def __init__(self, host, user, passwd, dbName='', charset='utf8'):
self.host=host
self.user=user
self.passwd=passwd
self.dbName=dbName
self.charset=charsetdefconnet(self):
self.db= pymysql.connect(host=self.host, user=self.user, passwd=self.passwd, db=self.dbName,
charset=self.charset) #连接数据库
self.cursor = self.db.cursor() #获取操作游标
defclose(self):
self.cursor.close()#释放游标
self.db.close() #关闭数据库连接
#查询
defget_all(self, sql):
res=Nonetry:
self.connet()
self.cursor.execute(sql)#执行sql语句
res = self.cursor.fetchall() #返回查询所有结果
exceptException as e:print('查询失败:%s' %e)finally:
self.close()returnres#增加、删除、修改
defshell_sql(self, sql):"执行sql语句"
print(sql)
count=0try:
self.connet()
count= self.cursor.execute(sql) #执行sql语句
self.db.commit() #提交
exceptException as e:print('事务提交失败:%s' %e)
self.db.rollback()#如果提交失败,回滚到上一次数据
finally:
self.close()returncountdef router_docx(choice1='', choice2='', choice3='', choice5='', choice6='', choice7='',paper_path='',name='1'):"生成网络通信方向试题及答案"docx1=Document()
docx2=Document()
docx1.styles['Normal'].font.name = '宋体' #选择字体
docx1.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体') #默认字体
docx1.styles['Normal'].font.size = Pt(11) #默认字号大小
docx1.styles['Normal'].paragraph_format.space_before = Pt(0) #默认段前间距
docx1.styles['Normal'].paragraph_format.space_after = Pt(0) #默认段后间距
docx1.styles['Normal'].paragraph_format.line_spacing_rule = WD_LINE_SPACING.ONE_POINT_FIVE #默认单倍行距
sec = docx1.sections[0] #sections对应文档中的“节”
sec.left_margin = Inches(1) #设置左页面边距
sec.right_margin = Inches(1) #设置右页面边距
sec.top_margin = Inches(0.5) #设置上页面边距
sec.bottom_margin = Inches(0.5) #设置下页面边距
p=docx1.add_paragraph() #添加段落
run = p.add_run('软件测试(网络通信)方向试题(%s)'