def WriteStuAndUinversityDatatoDoc(FilePathStu, iStu):
    '''
    功能:写doc文件总调用函数
    '''
    curospath = os.path.abspath('.')
    FilePathMBTI = curospath + '//data//MBTI_Data.xlsx'

    flag, MBTIData = rsui.ReadMBTIDataFromExcel(FilePathMBTI)
    if flag == 0:
        return 0
    #读取不同学生和学校的报考信息
    StuData, sugmajor, stumbti, stuanduniverinfo = dataprocess.GetAllStudentsUniversityandApplyConditions(
        FilePathStu, iStu)

    stunum = len(StuData)

    LoopList = []
    if len(iStu) == 1:
        if iStu[0] >= 0 and iStu[0] < stunum:
            LoopList.append(iStu[0])
        else:
            LoopList = range(stunum)
    else:
        #检查有效性
        for i in range(len(iStu)):
            if iStu[i] < 0 or iStu[i] >= stunum:
                del iStu[i]
        LoopList = iStu

    #word写入准备
    #一次打开word引擎
    #打开word引擎
    w = win32com.client.Dispatch('Word.Application')
    # 后台运行,不显示,不警告
    w.Visible = 1
    w.DisplayAlerts = 0

    ip = 0
    for i in LoopList:  #循环写入
        document = w.Documents.Add()  # 创建新的文档,对于每个学生创建一个新文档
        WriteFrontCoverPage(document, StuData[i])  #写封面页
        WriteFirstPage(document, StuData[i], MBTIData[stumbti[ip]],
                       sugmajor[ip])  #写第一页
        WriteUniversityInfoPage(document, stuanduniverinfo[ip])  #写学校的信息
        ip += 1
        docpath = curospath + '\\报告\\' + StuData[i][
            'name'] + '.docx'  #str(i) + '.docx'#
        docpath = docpath.decode('utf-8')
        document.SaveAs(docpath)
        document.Close()

    w.Quit()

    return 1
def WriteStuAndUinversityDatatoDoc(FilePathStu, iStu):
    '''
    功能:写doc文件总调用函数
    '''
    curospath = os.path.abspath('.')
    FilePathMBTI = curospath + '//data//MBTI_Data.xlsx'

    flag, MBTIData = rsui.ReadMBTIDataFromExcel(FilePathMBTI)
    if flag == 0:
        return 0
    #读取不同学生和学校的报考信息
    print FilePathStu
    StuData, sugmajor, stumbti, stuanduniverinfo = dataprocess.GetAllStudentsUniversityandApplyConditions(
        FilePathStu, iStu)

    stunum = len(StuData)

    LoopList = []
    if len(iStu) == 1:
        if iStu[0] >= 0 and iStu[0] < stunum:
            LoopList.append(iStu[0])
        else:
            LoopList = range(stunum)
    else:
        #检查有效性
        for i in range(len(iStu)):
            if iStu[i] < 0 or iStu[i] >= stunum:
                del iStu[i]
        LoopList = iStu

    ip = 0
    for i in LoopList:  #循环写入
        document = Document()
        WriteFrontCoverPage(document, StuData[i])  #写封面页
        WriteFirstPage(document, StuData[i], MBTIData[stumbti[ip]],
                       sugmajor[ip])  #写第一页
        WriteUniversityInfoPage(document, stuanduniverinfo[ip])  #写学校的信息
        ip += 1
        docpath = curospath + '\\报告\\' + StuData[i][
            'name'] + '.docx'  #str(i) + '.docx'#
        docpath = docpath.decode('utf-8')
        document.save(docpath)
    return 1
    def WriteStuAndUinversityDatatoDoc(self, FilePathStu, iStu, pdfflag):
        '''
        功能:写doc文件总调用函数
        '''
        #计算状态提示
        if len(iStu) == 1:
            blabel = u'生成报告'
            button = self.m_buttonCreateDocument
        else:
            blabel = u'批量生成报告'
            button = self.m_buttonCreateBatchDocument

        vstr = blabel + u'(初始化中...)'
        button.SetLabel(vstr)

        curospath = os.path.abspath('.')
        FilePathMBTI = curospath + '\\data\\MBTI_Data.xlsx'
        #创建一个新的目录用于存放生成的报告
        newdir = curospath + u'\\报告'.encode('gbk')
        if not os.path.exists(newdir):
            os.mkdir(newdir)

        flag, MBTIData = rsu.ReadMBTIDataFromExcel(FilePathMBTI)
        if flag == 0:
            return 0
        #读取不同学生和学校的报考信息
        StuData, sugmajor, stumbti, stuanduniverinfo = dataprocess.GetAllStudentsUniversityandApplyConditions(
            FilePathStu, iStu)

        stunum = len(StuData)

        LoopList = []
        if len(iStu) == 1:
            if iStu[0] >= 0 and iStu[0] < stunum:
                LoopList.append(iStu[0])
            else:
                LoopList = range(stunum)
        else:
            #检查有效性
            for i in range(len(iStu)):
                if iStu[i] < 0 or iStu[i] >= stunum:
                    del iStu[i]
            LoopList = iStu

        vstr = blabel + u'(' + str(0).decode('utf-8') + u'%...)'
        button.SetLabel(vstr)

        #如果要转为pdf文件,先启动wordapplication
        wordapp = wc.Dispatch('Word.Application')
        wordapp.Visible = 0
        wordapp.DisplayAlerts = 0

        ip = 0
        for i in LoopList:  #循环写入
            document = wordapp.Documents.Add()
            wdd.WriteFrontCoverPage(document, StuData[i])  #写封面页
            wdd.WriteFirstPage(document, StuData[i], MBTIData[stumbti[ip]],
                               sugmajor[ip])  #写第一页
            wdd.WriteUniversityInfoPage(document,
                                        stuanduniverinfo[ip])  #写学校的信息
            ip += 1
            docpath = newdir + '\\' + StuData[i]['name'].decode(
                'utf-8').encode('gbk') + '.docx'  #str(i) + '.docx'#
            document.SaveAs(docpath)

            #转为pdf格式
            if pdfflag:
                pdfpath = newdir + '\\' + StuData[i]['name'].decode(
                    'utf-8').encode('gbk') + '.pdf'
                document.SaveAs(pdfpath, 17)  #17对应于下表中的pdf文件
            document.Close()

            #将计算进度显示到按钮标签上
            prog = int(float(ip) / float(len(LoopList)) * 100)
            vstr = blabel + u'(' + str(prog).decode('utf-8') + u'%...)'
            button.SetLabel(vstr)

        wordapp.Quit()

        #完成,将标签置为初始值
        button.SetLabel(vstr)
        #提示信息
        txt = u'当前工作完成,总共导出' + str(len(LoopList)).decode('utf-8') + u'份报告!'
        wx.MessageBox(txt)

        return 1