admin管理员组

文章数量:821342

python 使用python

生成效果:

     

生成代码


from docx import Document
from docx.shared import Inches,Pt
from docx.oxml.ns import qn
from django.http import HttpResponse,FileResponse
import cStringIO, urllib2
import datetime
import osdef WordDownload(req):doc = Document()  # 建立模板对象chg_font(doc.styles['Normal'], fontname=u'宋体', size=12)  # 设置默认字体paragraph1 = doc.add_paragraph()run1 = paragraph1.add_run(u'基本信息')chg_font(run1, fontname=u'微软雅黑', size=16)paragraph2 = doc.add_paragraph()lister = [u'作者姓名:%s' % doctor_user.realname, u'所在地区:%s' % doctor_user.area,u'所在医院:%s' % doctor_user.hospital_name, u'所在科室:%s' %                         doctor_user.department,u'手机号码:%s' % doctor_user.telephone, u'个人积分:%s' % doctor_user.integral,u'作者照片:', u'作者简介:%s' % healer.introduce]for an in lister:  #if u'照片' in an:run = paragraph2.add_run(an)run.add_break()if wx_urls:file = urllib2.urlopen(wx_urls)tmpIm = cStringIO.StringIO(file.read())run.add_picture(tmpIm, width=Inches(0.8))else:paragraph2.add_run(u"没有照片")run.add_break()  # 添加换行else:run = paragraph2.add_run(an)run.add_break()# ---第一部分  基本信息 ----#paragraph1 = doc.add_paragraph()run1 = paragraph1.add_run(u'基本信息')chg_font(run1, fontname=u'微软雅黑', size=16)paragraph2 = doc.add_paragraph()lister = [u'作者姓名:%s' % doctor_user.realname, u'所在地区:%s' % doctor_user.area,u'所在医院:%s' % doctor_user.hospital_name, u'所在科室:%s' % doctor_user.department,u'手机号码:%s' % doctor_user.telephone, u'个人积分:%s' % doctor_user.integral,u'作者照片:', u'作者简介:%s' % healer.introduce]for an in lister:  #if u'照片' in an:run = paragraph2.add_run(an)run.add_break()if wx_urls:file = urllib2.urlopen(wx_urls)tmpIm = cStringIO.StringIO(file.read())run.add_picture(tmpIm, width=Inches(0.8))else:paragraph2.add_run(u"没有照片")run.add_break()  # 添加换行else:run = paragraph2.add_run(an)run.add_break()# ---第二部分  投稿详情 ----#paragraph3 = doc.add_paragraph()run3 = paragraph3.add_run(u'投稿详情')chg_font(run3, fontname=u'微软雅黑', size=16)paragraph4 = doc.add_paragraph()run = paragraph4.add_run(u'投稿标题:%s'%healer.title)run.add_break()# ---第三部分  投稿文字 ----#medical=MedicalNotes.objects.filter(healer=healer).first()paragraph5 = doc.add_paragraph()run5 = paragraph5.add_run(u'投稿文字')chg_font(run5, fontname=u'微软雅黑', size=16)paragraph6 = doc.add_paragraph()run = paragraph6.add_run(medical.content)run.add_break()# ---第四部分  投稿图片 ----#paragraph7 = doc.add_paragraph()run7 = paragraph7.add_run(u'投稿图片')chg_font(run7, fontname=u'微软雅黑', size=16)                    butelink=list(ContributeLink.objects.filter(medical=medical,typeser=1).values_list('links',flat=True))paragraph8 = doc.add_paragraph()run = paragraph8.add_run(u'投稿图片')run.add_break()for img in butelink:file = urllib2.urlopen(img + '?imageView2/1/w/800/h/700')tmpIm = cStringIO.StringIO(file.read())run.add_picture(tmpIm, width=Inches(0.8))run.add_break()# ---第五部分  审核记录 (生成表格的方式)----#paragraph9 = doc.add_paragraph()run9 = paragraph9.add_run(u'审核记录')chg_font(run9, fontname=u'微软雅黑', size=16)record = CaseAuditRecord.objects.filter(healer=healer)rows = record.count() + 1tab = doc.add_table(rows=rows, cols=4)tab1 = tab.rows[0].cellser = 0lister4 = [u'操作人', u'处理时间', u'操作', u'驳回原因']  # 表头for an in lister4:tab1[er].text = aner += 1if record:rowser = 1for an in record.values('staff__name', 'create_time', 'audit_type', 'reject'):tab2 = tab.rows[rowser].cellstab2[0].text = an['staff__name']tab2[1].text = an['create_time'].strftime('%Y-%m-%d %H:%M:%S')tab2[2].text = u'%s' % AUDIT_TYPE[int(an['audit_type'] - 1)][1]tab2[3].text = an['reject']rowser += 1run9.add_break()# ---第六部分  生成word数据流直接下载 ----#new_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')file_name='word%s.docx' % new_timedoc_url='./wordfile/'+file_namedoc.save(doc_url)docfile=open(doc_url,"rb")response=FileResponse(docfile)response['Content-Type']='application/octet-stream'response['Content-Disposition']='attachment;filename=word%s.docx'%new_timepath=os.getcwd()os_file_url=path+'/wordfile'w3 = threading.Thread(target=del_file, args=((os_file_url,file_name)))w3.start()return response

 

本文标签: python 使用python