Пример #1
0
    def save_worklog():
        """Create and save the worklog"""

        def row_to_dict(row):
            d = {
                'Date': str(row.Date.day),
                'Hours': str(row.Hours),
                'Description': row.Comments,
                'Task': ''
            }
            return d

        template = r'../templates/worklog_template.docx'

        document = MailMerge(template)

        # Fill the header
        document.merge(
            Name=name,
            Year=year,
            Month=month,
            Total_hours=str(total_hours),
        )

        # Fill the table
        # print(f"IRAP df hours: {irap_df.Hours}")
        irap_df.Hours = irap_df.Hours.replace(np.nan, 0)
        table_dict = irap_df.replace(np.nan, '').apply(row_to_dict, axis=1)
        document.merge_rows('Date', table_dict)

        file_name = f"{name} {month} {year} IRAP Worklog.docx"
        document.write(file_name)
        st.markdown(get_binary_file_downloader(file_name, file_name), unsafe_allow_html=True)
        document.close()
        print(f"Worklog save successful.")
    def makeReport(self, db, template):

        for rule_key in db.keys():
            print(rule_key)
            for did in db[rule_key].keys():
                document = MailMerge(template)
                print(did)
                dr_list = []
                i = 1
                for item in db[rule_key][did]:
                    d = {}
                    d['ListNo'] = str(i)
                    d['FileName'] = item[1]
                    d['FileLine'] = str(item[2])
                    t = catex.catex(d['FileName'])
                    d['FileContent'] = t.catex(item[2], 1, 1)
                    t.close()
                    dr_list.append(d)
                    i = i + 1

                document.merge(DR_Rule_Title=rule_key,
                               DR_Rule_Category=did,
                               DR_Reviewer=self._reviewer,
                               DR_Approver=self._approver,
                               DR_Writer=self._writer)
                document.merge_rows("ListNo", dr_list)
                document.write("result_%s.docx" % (rule_key + did))
                document.close()
Пример #3
0
 def write_final_options(self, name):
     template = "test-print.docx"
     document = MailMerge(template)
     document.merge_rows('item', docx_dict)
     document.merge(**merge2)
     if name.endswith('.docx'):
         document.write(name)
     else:
         document.write(name + '.docx')
     document.close()
Пример #4
0
def create_document(cfg, output="output.docx", cfg_date=False):
    template_docx = TEMPLATE_1
    if cfg_date:
        template_docx = TEMPLATE_2

    document = MailMerge(template_docx)
    # print(document.get_merge_fields())
    section_user = cfg["user"]
    section_bank = cfg["bank"]
    section_target = cfg["target"]
    section_service = cfg["service"]
    section_custom_date = cfg["custom_date"]

    if cfg_date:
        document.merge(
            amount=section_service["amount"],
            bankName=section_bank["bankName"],
            bankNum=str(section_bank["bankNum"]),
            business=section_target["business"],
            name=section_user["name"],
            nameUpper=section_user["name"].upper(),
            nit=section_target["nit"],
            nid=str(section_user["nid"]),
            nidCity=section_user["nidCity"],
            day=str(section_custom_date["day"]),
            dayStart=str(section_custom_date["dayStart"]),
            dayEnd=str(section_custom_date["dayEnd"]),
            month=section_custom_date["month"],
            monthStart=section_custom_date["monthStart"],
            monthEnd=section_custom_date["monthEnd"],
            year=str(section_custom_date["year"]),
            yearStart=str(section_custom_date["yearStart"]),
            yearEnd=str(section_custom_date["yearEnd"]),
        )
    else:
        formated_date, formated_date_start, formated_date_end = get_date()
        document.merge(
            amount=section_service["amount"],
            bankName=section_bank["bankName"],
            bankNum=str(section_bank["bankNum"]),
            business=section_target["business"],
            name=section_user["name"],
            nameUpper=section_user["name"].upper(),
            nit=section_target["nit"],
            nid=str(section_user["nid"]),
            nidCity=section_user["nidCity"],
            currentDate=formated_date,
            dateStart=formated_date_start,
            dateEnd=formated_date_end,
        )

    document.write(output)
    document.close()
class MergeTableRowsMultipartTest(EtreeMixin, unittest.TestCase):
    def setUp(self):
        self.document = MailMerge(path.join(path.dirname(__file__), 'test_merge_table_multipart.docx'))
        self.expected_xml = '<w:document xmlns:ns1="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p w:rsidP="00DA4DE0" w:rsidR="00231DA5" w:rsidRDefault="00DA4DE0"><w:pPr><w:pStyle w:val="Ttulo" /></w:pPr><w:r><w:t>Grades</w:t></w:r></w:p><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:r><w:t>Bouke Haarsma</w:t></w:r><w:r w:rsidR="002C29C5"><w:t xml:space="preserve"> </w:t></w:r><w:r w:rsidR="00DA4DE0"><w:t>received the grades</w:t></w:r><w:r w:rsidR="002C29C5"><w:t xml:space="preserve"> for </w:t></w:r><w:r><w:t /></w:r><w:r w:rsidR="00DA4DE0"><w:t xml:space="preserve"> in the table below.</w:t></w:r></w:p><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0" /><w:tbl><w:tblPr><w:tblStyle w:val="Sombreadoclaro-nfasis1" /><w:tblW w:type="auto" w:w="0" /><w:tblLook w:val="04E0" /></w:tblPr><w:tblGrid><w:gridCol w:w="1777" /><w:gridCol w:w="4894" /><w:gridCol w:w="1845" /></w:tblGrid><w:tr w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:val="100000000000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0"><w:r><w:t>Class Code</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0"><w:pPr><w:cnfStyle w:val="100000000000" /></w:pPr><w:r><w:t>Class Name</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0"><w:pPr><w:cnfStyle w:val="100000000000" /></w:pPr><w:r><w:t>Grade</w:t></w:r></w:p></w:tc></w:tr><w:tr w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:val="000000100000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:r><w:t>ECON101</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:pPr><w:cnfStyle w:val="000000100000" /></w:pPr><w:r><w:t>Economics 101</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:pPr><w:cnfStyle w:val="000000100000" /></w:pPr><w:r><w:t>A</w:t></w:r></w:p></w:tc></w:tr><w:tr w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:val="000000100000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:r><w:t>ECONADV</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:pPr><w:cnfStyle w:val="000000100000" /></w:pPr><w:r><w:t>Economics Advanced</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:pPr><w:cnfStyle w:val="000000100000" /></w:pPr><w:r><w:t>B</w:t></w:r></w:p></w:tc></w:tr><w:tr w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:val="000000100000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:r><w:t>OPRES</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:pPr><w:cnfStyle w:val="000000100000" /></w:pPr><w:r><w:t>Operations Research</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:pPr><w:cnfStyle w:val="000000100000" /></w:pPr><w:r><w:t>A</w:t></w:r></w:p></w:tc></w:tr><w:tr w:rsidR="00C829DD" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:val="010000000000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00C829DD" w:rsidRDefault="00C829DD"><w:r><w:t>THESIS</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00C829DD" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:val="010000000000" /></w:pPr><w:r><w:t>Final thesis</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00C829DD" w:rsidRDefault="0037127B"><w:pPr><w:cnfStyle w:val="010000000000" /></w:pPr><w:r><w:t>A</w:t></w:r></w:p></w:tc></w:tr></w:tbl><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0" w:rsidRPr="00DA4DE0"><w:bookmarkStart w:id="0" w:name="_GoBack" /><w:bookmarkEnd w:id="0" /></w:p><w:sectPr w:rsidR="00DA4DE0" w:rsidRPr="00DA4DE0" w:rsidSect="003B4151"><w:headerReference ns1:id="rId7" w:type="default" /><w:pgSz w:h="16840" w:w="11900" /><w:pgMar w:bottom="1440" w:footer="708" w:gutter="0" w:header="708" w:left="1800" w:right="1800" w:top="1672" /><w:cols w:space="708" /><w:docGrid w:linePitch="360" /></w:sectPr></w:body></w:document>'
        self.expected_tree = etree.fromstring(self.expected_xml) 

    def test_merge_rows_on_multipart_file(self):
        self.assertEqual(self.document.get_merge_fields(),
                         {'student_name', 'study_name', 'class_name', 'class_code', 'class_grade', 'thesis_grade'})

        self.document.merge(
            student_name='Bouke Haarsma',
            study='Industrial Engineering and Management',
            thesis_grade='A',
        )

        self.document.merge_rows('class_code', [
            {'class_code': 'ECON101', 'class_name': 'Economics 101', 'class_grade': 'A'},
            {'class_code': 'ECONADV', 'class_name': 'Economics Advanced', 'class_grade': 'B'},
            {'class_code': 'OPRES', 'class_name': 'Operations Research', 'class_grade': 'A'},
        ])

        with tempfile.TemporaryFile() as outfile:
            self.document.write(outfile)

        for part in self.document.parts.values():
            # only check the document part 
            if (part.getroot().tag == '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}document'):
                self.assert_equal_tree(self.expected_tree, part.getroot())

    def test_merge_unified_on_multipart_file(self):
        self.document.merge(
            student_name='Bouke Haarsma',
            study='Industrial Engineering and Management',
            thesis_grade='A',
            class_code=[
                {'class_code': 'ECON101', 'class_name': 'Economics 101', 'class_grade': 'A'},
                {'class_code': 'ECONADV', 'class_name': 'Economics Advanced', 'class_grade': 'B'},
                {'class_code': 'OPRES', 'class_name': 'Operations Research', 'class_grade': 'A'},
            ]
        )

        with tempfile.TemporaryFile() as outfile:
            self.document.write(outfile)

        for part in self.document.parts.values():
            # only check the document part 
            if (part.getroot().tag == '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}document'):
                self.assert_equal_tree(self.expected_tree, part.getroot())

    def tearDown(self):
        self.document.close()
Пример #6
0
 def create_word_report(self, ids, data, report):
     self.parser_instance = self.parser(self.env.cr, self.env.uid,
                                        self.name2, self.env.context)
     objs = self.getObjects(self.env.cr, self.env.uid, ids,
                            self.env.context)
     self.parser_instance.set_context(objs, data, ids, 'docx')
     file_data = cStringIO.StringIO()
     document = MailMerge(template)
     self.generate_word_report(document, data, objs, report)
     document.write(file_data)
     document.close()
     file_data.seek(0)
     return (file_data.read(), 'docx')
Пример #7
0
    def apply_mailmerge(self, context, filename="example.docx"):
        # Return a django request response with a docx download.
        docx = StringIO()

        mailmerge = MailMerge(self.get_full_path())
        mailmerge.merge(context)
        mailmerge.write(docx)
        mailmerge.close()

        # return response
        docx.seek(0)
        response = HttpResponse(docx)
        response['Content-Disposition'] = 'attachment; filename={}'.format(filename)
        response['Content-Type'] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
        return response
Пример #8
0
inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
                                    # the inbox. 
messages = inbox.Items.Restrict("[ReceivedTime] >= '" + str(datetime.date.today()) + "'")

for message in reversed(messages):
    if loop_run:
        break
    subject = message.subject
    Ex_extract = False
    if re.search("Mail Subject", str(subject)) :      
        body = message.Body
        body_list = body.splitlines()
        
        for val in body_list:
            if Ex_extract:
                Ex_list.append(val)
            if re.search("Example :", val):
                Ex_extract = True
                
        loop_run = True
        
template = 'ABC_Template.docx'
document = MailMerge(template)
print(document.get_merge_fields())

document.merge(
    Example = ', '.join(Ex_list)
    )
document.write('ABC.docx')
document.close()
class MergeTableRowsTest(EtreeMixin, unittest.TestCase):
    def setUp(self):
        self.document = MailMerge(
            path.join(path.dirname(__file__), 'test_merge_table_rows.docx'))
        self.expected_tree = etree.fromstring(
            '<w:document xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ns1="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" mc:Ignorable="w14 wp14"><w:body><w:p ns1:paraId="28ACC80D" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00231DA5" w:rsidRDefault="00DA4DE0"><w:pPr><w:pStyle w:val="Title" /></w:pPr><w:proofErr w:type="spellStart" /><w:r><w:t>Grades</w:t></w:r><w:proofErr w:type="spellEnd" /></w:p><w:p ns1:paraId="07836F52" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:r><w:t>Bouke Haarsma</w:t></w:r><w:r w:rsidR="002C29C5"><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart" /><w:r w:rsidR="00DA4DE0"><w:t>received</w:t></w:r><w:proofErr w:type="spellEnd" /><w:r w:rsidR="00DA4DE0"><w:t xml:space="preserve"> the </w:t></w:r><w:proofErr w:type="spellStart" /><w:r w:rsidR="00DA4DE0"><w:t>grades</w:t></w:r><w:proofErr w:type="spellEnd" /><w:r w:rsidR="002C29C5"><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart" /><w:r w:rsidR="002C29C5"><w:t>for</w:t></w:r><w:proofErr w:type="spellEnd" /><w:r w:rsidR="002C29C5"><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t /></w:r><w:r w:rsidR="00DA4DE0"><w:t xml:space="preserve"> in the </w:t></w:r><w:proofErr w:type="spellStart" /><w:r w:rsidR="00DA4DE0"><w:t>table</w:t></w:r><w:proofErr w:type="spellEnd" /><w:r w:rsidR="00DA4DE0"><w:t xml:space="preserve"> below.</w:t></w:r></w:p><w:p ns1:paraId="58525309" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0" /><w:tbl><w:tblPr><w:tblStyle w:val="LightShading-Accent1" /><w:tblW w:type="auto" w:w="0" /><w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="0" w:lastRow="1" w:noHBand="0" w:noVBand="1" w:val="04E0" /></w:tblPr><w:tblGrid><w:gridCol w:w="1777" /><w:gridCol w:w="4894" /><w:gridCol w:w="1845" /></w:tblGrid><w:tr ns1:paraId="599252DD" ns1:textId="77777777" w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="1" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="100000000000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="1" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p ns1:paraId="69486D96" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0"><w:r><w:t>Class Code</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p ns1:paraId="1AA11439" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="1" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="100000000000" /></w:pPr><w:r><w:t>Class Name</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p ns1:paraId="3091846F" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="1" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="100000000000" /></w:pPr><w:r><w:t>Grade</w:t></w:r></w:p></w:tc></w:tr><w:tr ns1:paraId="1699D5B6" ns1:textId="77777777" w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="1" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p ns1:paraId="5D81DF7F" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:r><w:t>ECON101</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p ns1:paraId="5A67E49A" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:pPr><w:r><w:t>Economics 101</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p ns1:paraId="5EB9BD23" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:pPr><w:r><w:t>A</w:t></w:r></w:p></w:tc></w:tr><w:tr ns1:paraId="1699D5B6" ns1:textId="77777777" w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="1" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p ns1:paraId="5D81DF7F" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:r><w:t>ECONADV</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p ns1:paraId="5A67E49A" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:pPr><w:r><w:t>Economics Advanced</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p ns1:paraId="5EB9BD23" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:pPr><w:r><w:t>B</w:t></w:r></w:p></w:tc></w:tr><w:tr ns1:paraId="1699D5B6" ns1:textId="77777777" w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="1" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p ns1:paraId="5D81DF7F" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:r><w:t>OPRES</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p ns1:paraId="5A67E49A" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:pPr><w:r><w:t>Operations Research</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p ns1:paraId="5EB9BD23" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:pPr><w:r><w:t>A</w:t></w:r></w:p></w:tc></w:tr><w:tr ns1:paraId="0B5730FD" ns1:textId="77777777" w:rsidR="00C829DD" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="1" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="010000000000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="1" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p ns1:paraId="6A211A5A" ns1:textId="4E90EB38" w:rsidP="00DA4DE0" w:rsidR="00C829DD" w:rsidRDefault="00C829DD"><w:r><w:t>THESIS</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p ns1:paraId="12FCE443" ns1:textId="289CA6C6" w:rsidP="00DA4DE0" w:rsidR="00C829DD" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="1" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="010000000000" /></w:pPr><w:proofErr w:type="spellStart" /><w:r><w:t>Final</w:t></w:r><w:proofErr w:type="spellEnd" /><w:r><w:t xml:space="preserve"> thesis</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p ns1:paraId="0ACD9198" ns1:textId="413A3A4D" w:rsidP="00DA4DE0" w:rsidR="00C829DD" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="1" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="010000000000" /></w:pPr><w:r><w:t>A</w:t></w:r></w:p></w:tc></w:tr></w:tbl><w:p ns1:paraId="26A87379" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0" w:rsidRPr="00DA4DE0"><w:bookmarkStart w:id="0" w:name="_GoBack" /><w:bookmarkEnd w:id="0" /></w:p><w:sectPr w:rsidR="00DA4DE0" w:rsidRPr="00DA4DE0" w:rsidSect="002C0659"><w:pgSz w:h="16840" w:w="11900" /><w:pgMar w:bottom="1440" w:footer="708" w:gutter="0" w:header="708" w:left="1800" w:right="1800" w:top="1440" /><w:cols w:space="708" /><w:docGrid w:linePitch="360" /></w:sectPr></w:body></w:document>'
        )  # noqa

    def test_merge_rows(self):
        self.assertEqual(
            self.document.get_merge_fields(), {
                'student_name', 'study_name', 'class_name', 'class_code',
                'class_grade', 'thesis_grade'
            })

        self.document.merge(
            student_name='Bouke Haarsma',
            study='Industrial Engineering and Management',
            thesis_grade='A',
        )

        self.document.merge_rows('class_code', [
            {
                'class_code': 'ECON101',
                'class_name': 'Economics 101',
                'class_grade': 'A'
            },
            {
                'class_code': 'ECONADV',
                'class_name': 'Economics Advanced',
                'class_grade': 'B'
            },
            {
                'class_code': 'OPRES',
                'class_name': 'Operations Research',
                'class_grade': 'A'
            },
        ])

        with tempfile.TemporaryFile() as outfile:
            self.document.write(outfile)

        self.assert_equal_tree(self.expected_tree,
                               list(self.document.parts.values())[0].getroot())

    def test_merge_rows_no_table(self):
        """
        Merging of rows should not fail if there is no table with the given
        column in the document
        """
        self.document.merge(student_name='Bouke Haarsma',
                            study='Industrial Engineering and Management',
                            thesis_grade='A',
                            class_code=[
                                {
                                    'class_code': 'ECON101',
                                    'class_name': 'Economics 101',
                                    'class_grade': 'A'
                                },
                                {
                                    'class_code': 'ECONADV',
                                    'class_name': 'Economics Advanced',
                                    'class_grade': 'B'
                                },
                                {
                                    'class_code': 'OPRES',
                                    'class_name': 'Operations Research',
                                    'class_grade': 'A'
                                },
                            ],
                            no_table=[{
                                'no_table': 'Table not available'
                            }])

        with tempfile.TemporaryFile() as outfile:
            self.document.write(outfile)

        self.assert_equal_tree(self.expected_tree,
                               list(self.document.parts.values())[0].getroot())

    def test_merge_rows_remove_table(self):
        """
        When flag is set and there is no data in a table, table needs to be removed
        """
        self.document.remove_empty_tables = True
        self.document.merge(student_name='Bouke Haarsma',
                            study='Industrial Engineering and Management',
                            thesis_grade='A',
                            class_code=[])

        with tempfile.TemporaryFile() as outfile:
            self.document.write(outfile)
        self.assertIsNone(
            list(self.document.parts.values())[0].getroot().find(
                './/{%(w)s}tbl' % NAMESPACES))

    def test_merge_unified(self):
        self.document.merge(student_name='Bouke Haarsma',
                            study='Industrial Engineering and Management',
                            thesis_grade='A',
                            class_code=[
                                {
                                    'class_code': 'ECON101',
                                    'class_name': 'Economics 101',
                                    'class_grade': 'A'
                                },
                                {
                                    'class_code': 'ECONADV',
                                    'class_name': 'Economics Advanced',
                                    'class_grade': 'B'
                                },
                                {
                                    'class_code': 'OPRES',
                                    'class_name': 'Operations Research',
                                    'class_grade': 'A'
                                },
                            ])

        with tempfile.TemporaryFile() as outfile:
            self.document.write(outfile)

        self.assert_equal_tree(self.expected_tree,
                               list(self.document.parts.values())[0].getroot())

    def tearDown(self):
        self.document.close()
    def get_file(self, n_idx, n_row):
        list_columns = ['Global ID', 'Local Name', 'Status', 'Notes']
        df_list = pd.DataFrame(columns=list_columns)
        lv_template = self.template
        document_1 = MailMerge(self.path + lv_template)

        try:

            dict = {
                "Global_ID":
                str(n_row['Global ID']),
                "English_Name":
                str(n_row['English Name']),
                "Local_Name":
                str(n_row['Local Name']),
                "Department":
                str(n_row['Department']),
                "Performance_Rating":
                n_row['Performance Rating'],
                "Bonus_Amount":
                '{:,.2f}'.format(n_row['Bonus_Amount']),
                "Individual_Modifier":
                '{:.2f}'.format(n_row['Individual Modifier']),
                # "Individual_Modifier": '{:.2f}%'.format(n_row['Individual Modifier']),
                "Business_Modifier":
                '{:.2f}'.format(n_row['Business Modifier']),
                "Payout_Rate":
                '{:.2f}'.format(n_row['Payout Rate']),
            }
            document_1.merge(**dict)

            file_path = self.path + 'xlsx/'
            if not os.path.exists(file_path):
                os.makedirs(file_path)
            # filename
            lv_name = self.prefix + str(
                n_row['Global ID']) + '_' + n_row['Local Name']
            file_docx = self.path + 'xlsx/' + lv_name + '.docx'
            document_1.write(file_docx)
            # time.sleep(1)

            file_path = self.path + 'pdf' + '/' + n_row['HRBP'] + '/'
            if not os.path.exists(file_path):
                os.makedirs(file_path)

            if n_row['Division'] != "":
                file_path = file_path + n_row['Division'] + '/'
                if not os.path.exists(file_path):
                    os.makedirs(file_path)

            file_pdf = file_path + lv_name + '.pdf'

            convert(file_docx, file_pdf, keep_active=True)
            print('Success:', n_row['Local Name'], n_idx + 1)
            document_1.close()

            emp_line = [n_row['Global ID'], n_row['Local Name'], 'S', '']
            df_list = df_list.append(pd.Series(emp_line, index=list_columns),
                                     ignore_index=True)

        except Exception as e:
            print('Failed to generate file for line ', n_idx + 1, e)
            emp_line = [n_row['Global ID'], n_row['Local Name'], 'E', '']
            df_list = df_list.append(pd.Series(emp_line, index=list_columns),
                                     ignore_index=True)
        df_list.to_csv(self.path + 'log.csv')
Пример #11
0
            str_total_surcharge = f'{total_surcharge:.2f}'
            merge_match_results.append(str_total_surcharge)
            sheet3.append(merge_match_results)      # dump data into summary excel sheet

            # combine the two lists into a dictionary so you can pass it to the word document
            merge_dict = {MERGE_FIELD_LABELS[i]: merge_match_results[i] for i in range(len(MERGE_FIELD_LABELS))}

            # append the combined keys and values to a list from a separate python file
            monthly_surcharges.append(dict(merge_dict))

            document_1.merge_templates([merge_dict], "page_break")
            # document_1.merge_pages([merge_dict])
            # NOTE if I was going to combine all the pages into
            # one document I would use above
            document_1.write("./output/" + document_title)
            document_1.close()
            # taking the just written document as in_file so it can be reopened and saved as a pdf
            in_file = PROJECT_FOLDER_OUTPUT + document_title
            out_file = PROJECT_FOLDER_OUTPUT + document_title2

            # creating COM object
            word = comtypes.client.CreateObject('Word.Application')
            # word.Visible = True  # kind of annoying having every document window pop up
            time.sleep(1)  # this slows down the file generation but avoids errors
            doc = word.Documents.Open(in_file)  # in_file is the word document just created
            doc.SaveAs(out_file, FileFormat=WRD_TO_PDF)  # out_file makes the pdf
            doc.Close()
            word.Quit()
            # word.Visible = False
            # set the column_start to grab the next set of columns on the row
            column_start = column_start + 6
    trknbylo = str(trknbeen),
    vkknbylo = str(vkldbeen),
    month2 = str(report_month),
    allblanks = str(allblanks),
    dtmin = str(dtmin),
    dtmax = str(dtmax),
    dtcount = str(dtcount),
    dtcountprice = str(float("%.2f" % (trknbeen*1.29))),
    dtpriceallsell = str(dtpriceallsell),
    dtostatok = str(dtostatok),
    dtostatokprice = str(float("%.2f" % (dtostatok*1.29))),
    dvmin = str(dvmin),
    dvmax = str(dvmax),
    dvcount = str(dvcount),
    dvpriceallsell = str(dvpriceallsell),
    dvostatok = str(dvostatok),
    dvcountprice = str(float("%.2f" % (vkldbeen*0.01))),
    dvostatokprice = str(float("%.2f" % (dvostatok*0.01))),
    propis = str(num2words(allblanks))
)
filename = "Report BSR for " + report_month + " " + date.strftime(date.today(), "%Y") + ".docx"
document.write(path + filename)  # save file to folder
document.close() # close document

# remove temporary file with report
os.remove(path + ("Report BSR for " + report_month + " month [templ].docx"))


print(filename)
input("Press ENTER to exit from app...")
os.startfile(path)
Пример #13
0
class MergeTableRowsMultipartTest(EtreeMixin, unittest.TestCase):
    def setUp(self):
        self.document = MailMerge(
            path.join(path.dirname(__file__),
                      'test_merge_table_multipart.docx'))
        self.expected_xml = '<w:document xmlns:ns1="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p w:rsidP="00DA4DE0" w:rsidR="00231DA5" w:rsidRDefault="00DA4DE0"><w:pPr><w:pStyle w:val="Ttulo" /></w:pPr><w:r><w:t>Grades</w:t></w:r></w:p><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:r><w:t>Bouke Haarsma</w:t></w:r><w:r w:rsidR="002C29C5"><w:t xml:space="preserve"> </w:t></w:r><w:r w:rsidR="00DA4DE0"><w:t>received the grades</w:t></w:r><w:r w:rsidR="002C29C5"><w:t xml:space="preserve"> for </w:t></w:r><w:r><w:t /></w:r><w:r w:rsidR="00DA4DE0"><w:t xml:space="preserve"> in the table below.</w:t></w:r></w:p><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0" /><w:tbl><w:tblPr><w:tblStyle w:val="Sombreadoclaro-nfasis1" /><w:tblW w:type="auto" w:w="0" /><w:tblLook w:val="04E0" /></w:tblPr><w:tblGrid><w:gridCol w:w="1777" /><w:gridCol w:w="4894" /><w:gridCol w:w="1845" /></w:tblGrid><w:tr w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:val="100000000000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0"><w:r><w:t>Class Code</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0"><w:pPr><w:cnfStyle w:val="100000000000" /></w:pPr><w:r><w:t>Class Name</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0"><w:pPr><w:cnfStyle w:val="100000000000" /></w:pPr><w:r><w:t>Grade</w:t></w:r></w:p></w:tc></w:tr><w:tr w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:val="000000100000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:r><w:t>ECON101</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:pPr><w:cnfStyle w:val="000000100000" /></w:pPr><w:r><w:t>Economics 101</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:pPr><w:cnfStyle w:val="000000100000" /></w:pPr><w:r><w:t>A</w:t></w:r></w:p></w:tc></w:tr><w:tr w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:val="000000100000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:r><w:t>ECONADV</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:pPr><w:cnfStyle w:val="000000100000" /></w:pPr><w:r><w:t>Economics Advanced</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:pPr><w:cnfStyle w:val="000000100000" /></w:pPr><w:r><w:t>B</w:t></w:r></w:p></w:tc></w:tr><w:tr w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:val="000000100000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:r><w:t>OPRES</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:pPr><w:cnfStyle w:val="000000100000" /></w:pPr><w:r><w:t>Operations Research</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="0037127B"><w:pPr><w:cnfStyle w:val="000000100000" /></w:pPr><w:r><w:t>A</w:t></w:r></w:p></w:tc></w:tr><w:tr w:rsidR="00C829DD" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:val="010000000000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00C829DD" w:rsidRDefault="00C829DD"><w:r><w:t>THESIS</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00C829DD" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:val="010000000000" /></w:pPr><w:r><w:t>Final thesis</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p w:rsidP="00DA4DE0" w:rsidR="00C829DD" w:rsidRDefault="0037127B"><w:pPr><w:cnfStyle w:val="010000000000" /></w:pPr><w:r><w:t>A</w:t></w:r></w:p></w:tc></w:tr></w:tbl><w:p w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0" w:rsidRPr="00DA4DE0"><w:bookmarkStart w:id="0" w:name="_GoBack" /><w:bookmarkEnd w:id="0" /></w:p><w:sectPr w:rsidR="00DA4DE0" w:rsidRPr="00DA4DE0" w:rsidSect="003B4151"><w:headerReference ns1:id="rId7" w:type="default" /><w:pgSz w:h="16840" w:w="11900" /><w:pgMar w:bottom="1440" w:footer="708" w:gutter="0" w:header="708" w:left="1800" w:right="1800" w:top="1672" /><w:cols w:space="708" /><w:docGrid w:linePitch="360" /></w:sectPr></w:body></w:document>'
        self.expected_tree = etree.fromstring(self.expected_xml)

    def test_merge_rows_on_multipart_file(self):
        self.assertEqual(
            self.document.get_merge_fields(), {
                'student_name', 'study_name', 'class_name', 'class_code',
                'class_grade', 'thesis_grade'
            })

        self.document.merge(
            student_name='Bouke Haarsma',
            study='Industrial Engineering and Management',
            thesis_grade='A',
        )

        self.document.merge_rows('class_code', [
            {
                'class_code': 'ECON101',
                'class_name': 'Economics 101',
                'class_grade': 'A'
            },
            {
                'class_code': 'ECONADV',
                'class_name': 'Economics Advanced',
                'class_grade': 'B'
            },
            {
                'class_code': 'OPRES',
                'class_name': 'Operations Research',
                'class_grade': 'A'
            },
        ])

        with tempfile.TemporaryFile() as outfile:
            self.document.write(outfile)

        for part in self.document.parts.values():
            # only check the document part
            if (part.getroot().tag ==
                    '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}document'
                ):
                self.assert_equal_tree(self.expected_tree, part.getroot())

    def test_merge_unified_on_multipart_file(self):
        self.document.merge(student_name='Bouke Haarsma',
                            study='Industrial Engineering and Management',
                            thesis_grade='A',
                            class_code=[
                                {
                                    'class_code': 'ECON101',
                                    'class_name': 'Economics 101',
                                    'class_grade': 'A'
                                },
                                {
                                    'class_code': 'ECONADV',
                                    'class_name': 'Economics Advanced',
                                    'class_grade': 'B'
                                },
                                {
                                    'class_code': 'OPRES',
                                    'class_name': 'Operations Research',
                                    'class_grade': 'A'
                                },
                            ])

        with tempfile.TemporaryFile() as outfile:
            self.document.write(outfile)

        for part in self.document.parts.values():
            # only check the document part
            if (part.getroot().tag ==
                    '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}document'
                ):
                self.assert_equal_tree(self.expected_tree, part.getroot())

    def tearDown(self):
        self.document.close()
Пример #14
0
     'noprior': noprior
 }  # add prior email template and mid/start templates
 for d in name_role:
     welcome_email = email_types[d['etype']]
     welcome_letter = path2 + 'Template Welcome Letter-{}.docx'.format(
         d['template_flag'])
     doc_letter = MailMerge(welcome_letter)
     doc_cif = MailMerge(cif)
     name1 = outpath + d['letter']
     name2 = outpath + d['cif']
     print('Package complete for', d['Last_Name'])
     doc_letter.merge_pages([d])
     doc_cif.merge_pages([d])
     doc_letter.write(name1)
     doc_cif.write(name2)
     doc_letter.close()
     doc_cif.close()
     outlook = win32.Dispatch('outlook.application')
     recipient = d['Last_Name']
     prot = d['Protocol_Number']
     titl = d['Protocol_Full_Title']
     cmt = d['Committee']
     cmt_abrv = d['Committee_Type']
     due_date = datetime.now() + timedelta(days=7)
     due_date_formatted = due_date.strftime('%d-%b-%y')
     document1 = Document(outpath + d['letter'])
     core_properties1 = document1.core_properties
     core_properties1.author = d['Protocol_Number']
     document1.save(outpath + d['letter'])
     document2 = Document(outpath + d['cif'])
     core_properties2 = document2.core_properties
Пример #15
0
class handle_merge():
    def __init__(self):
        self.df_data = pd.DataFrame()
        self.template = 'C:/temp/mm/Test.docx'
        self.datafile = 'C:/temp/mm/Test.xlsx'
        self.path = 'C:/temp/mm/'
        self.document_1 = MailMerge(self.template)

    def read_files(self, ):
        # df_data = pd.DataFrame()
        try:
            self.df_data = pd.DataFrame(
                pd.read_excel(
                    io=self.datafile,
                    sheet_name='Sheet1',
                    # dtype={'ZF Global ID': 'str'},
                    header=0,
                    # skiprows=2))
                ))
            print(self.df_data.head())
        except Exception as e:
            print('Handle Employee Data Exception:', self.datafile, e)

    # def generate_pdf(self,docfile, pdf_file):
    #     # wdFormatPDF = 17
    #     word = DispatchEx("Word.Application")
    #     try:
    #         doc = word.Documents.Open(docfile, ReadOnly=1)
    #         # doc.SaveAs(pdf_file, FileFormat=wdFormatPDF)
    #         doc.ExportAsFixedFormat(OutputFileName=pdf_file,
    #                                 ExportFormat=17,  # 17 = PDF output, 18=XPS output
    #                                 OpenAfterExport=False,
    #                                 OptimizeFor=0,  # 0=Print (higher res), 1=Screen (lower res)
    #                                 CreateBookmarks=1,
    #                                 # 0=No bookmarks, 1=Heading bookmarks only, 2=bookmarks match word bookmarks
    #                                 DocStructureTags=True
    #                                 );
    #         doc.Close()
    #     except Exception as e:
    #         print('convert pdf failed,', docfile)

    def main(self):
        print(self.document_1.get_merge_fields())
        self.read_files()

        for n_idx, n_row in self.df_data.iterrows():
            dict = {
                "姓名": str(n_row['姓名']),
                "年龄": str(n_row['年龄']),
                "生日": str(n_row['生日']),
                "folder": 'folder1'
            }
            self.document_1.merge(**dict)

            file_path = self.path + 'xlsx/'
            if not os.path.exists(file_path):
                os.makedirs(file_path)

            file_docx = self.path + 'xlsx/' + n_row['姓名'] + '.docx'
            self.document_1.write(file_docx)
            # time.sleep(1)

            file_path = self.path + n_row['性别'] + '/'
            if not os.path.exists(file_path):
                os.makedirs(file_path)

            file_pdf = file_path + n_row['姓名'] + '.pdf'
            # file_pdf = self.path + 'pdfs/' + n_row['姓名'] + '.pdf'
            # self.generate_pdf(file_docx,file_pdf)
            convert(file_docx, file_pdf, keep_active=True)
        self.document_1.close()
Пример #16
0
class MergeTableRowsTest(EtreeMixin, unittest.TestCase):
    def setUp(self):
        self.document = MailMerge(path.join(path.dirname(__file__), 'test_merge_table_rows.docx'))
        self.expected_tree = etree.fromstring('<w:document xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ns1="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" mc:Ignorable="w14 wp14"><w:body><w:p ns1:paraId="28ACC80D" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00231DA5" w:rsidRDefault="00DA4DE0"><w:pPr><w:pStyle w:val="Title" /></w:pPr><w:proofErr w:type="spellStart" /><w:r><w:t>Grades</w:t></w:r><w:proofErr w:type="spellEnd" /></w:p><w:p ns1:paraId="07836F52" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:r><w:t>Bouke Haarsma</w:t></w:r><w:r w:rsidR="002C29C5"><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart" /><w:r w:rsidR="00DA4DE0"><w:t>received</w:t></w:r><w:proofErr w:type="spellEnd" /><w:r w:rsidR="00DA4DE0"><w:t xml:space="preserve"> the </w:t></w:r><w:proofErr w:type="spellStart" /><w:r w:rsidR="00DA4DE0"><w:t>grades</w:t></w:r><w:proofErr w:type="spellEnd" /><w:r w:rsidR="002C29C5"><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart" /><w:r w:rsidR="002C29C5"><w:t>for</w:t></w:r><w:proofErr w:type="spellEnd" /><w:r w:rsidR="002C29C5"><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t /></w:r><w:r w:rsidR="00DA4DE0"><w:t xml:space="preserve"> in the </w:t></w:r><w:proofErr w:type="spellStart" /><w:r w:rsidR="00DA4DE0"><w:t>table</w:t></w:r><w:proofErr w:type="spellEnd" /><w:r w:rsidR="00DA4DE0"><w:t xml:space="preserve"> below.</w:t></w:r></w:p><w:p ns1:paraId="58525309" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0" /><w:tbl><w:tblPr><w:tblStyle w:val="LightShading-Accent1" /><w:tblW w:type="auto" w:w="0" /><w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="0" w:lastRow="1" w:noHBand="0" w:noVBand="1" w:val="04E0" /></w:tblPr><w:tblGrid><w:gridCol w:w="1777" /><w:gridCol w:w="4894" /><w:gridCol w:w="1845" /></w:tblGrid><w:tr ns1:paraId="599252DD" ns1:textId="77777777" w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="1" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="100000000000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="1" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p ns1:paraId="69486D96" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0"><w:r><w:t>Class Code</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p ns1:paraId="1AA11439" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="1" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="100000000000" /></w:pPr><w:r><w:t>Class Name</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p ns1:paraId="3091846F" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="1" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="100000000000" /></w:pPr><w:r><w:t>Grade</w:t></w:r></w:p></w:tc></w:tr><w:tr ns1:paraId="1699D5B6" ns1:textId="77777777" w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="1" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p ns1:paraId="5D81DF7F" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:r><w:t>ECON101</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p ns1:paraId="5A67E49A" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:pPr><w:r><w:t>Economics 101</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p ns1:paraId="5EB9BD23" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:pPr><w:r><w:t>A</w:t></w:r></w:p></w:tc></w:tr><w:tr ns1:paraId="1699D5B6" ns1:textId="77777777" w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="1" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p ns1:paraId="5D81DF7F" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:r><w:t>ECONADV</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p ns1:paraId="5A67E49A" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:pPr><w:r><w:t>Economics Advanced</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p ns1:paraId="5EB9BD23" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:pPr><w:r><w:t>B</w:t></w:r></w:p></w:tc></w:tr><w:tr ns1:paraId="1699D5B6" ns1:textId="77777777" w:rsidR="00DA4DE0" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="1" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p ns1:paraId="5D81DF7F" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:r><w:t>OPRES</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p ns1:paraId="5A67E49A" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:pPr><w:r><w:t>Operations Research</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p ns1:paraId="5EB9BD23" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="1" w:oddVBand="0" w:val="000000100000" /></w:pPr><w:r><w:t>A</w:t></w:r></w:p></w:tc></w:tr><w:tr ns1:paraId="0B5730FD" ns1:textId="77777777" w:rsidR="00C829DD" w:rsidTr="00C829DD"><w:trPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="1" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="010000000000" /></w:trPr><w:tc><w:tcPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="1" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="001000000000" /><w:tcW w:type="dxa" w:w="1809" /></w:tcPr><w:p ns1:paraId="6A211A5A" ns1:textId="4E90EB38" w:rsidP="00DA4DE0" w:rsidR="00C829DD" w:rsidRDefault="00C829DD"><w:r><w:t>THESIS</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="5529" /></w:tcPr><w:p ns1:paraId="12FCE443" ns1:textId="289CA6C6" w:rsidP="00DA4DE0" w:rsidR="00C829DD" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="1" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="010000000000" /></w:pPr><w:proofErr w:type="spellStart" /><w:r><w:t>Final</w:t></w:r><w:proofErr w:type="spellEnd" /><w:r><w:t xml:space="preserve"> thesis</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:type="dxa" w:w="1178" /></w:tcPr><w:p ns1:paraId="0ACD9198" ns1:textId="413A3A4D" w:rsidP="00DA4DE0" w:rsidR="00C829DD" w:rsidRDefault="00C829DD"><w:pPr><w:cnfStyle w:evenHBand="0" w:evenVBand="0" w:firstColumn="0" w:firstRow="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastColumn="0" w:lastRow="1" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" w:oddHBand="0" w:oddVBand="0" w:val="010000000000" /></w:pPr><w:r><w:t>A</w:t></w:r></w:p></w:tc></w:tr></w:tbl><w:p ns1:paraId="26A87379" ns1:textId="77777777" w:rsidP="00DA4DE0" w:rsidR="00DA4DE0" w:rsidRDefault="00DA4DE0" w:rsidRPr="00DA4DE0"><w:bookmarkStart w:id="0" w:name="_GoBack" /><w:bookmarkEnd w:id="0" /></w:p><w:sectPr w:rsidR="00DA4DE0" w:rsidRPr="00DA4DE0" w:rsidSect="002C0659"><w:pgSz w:h="16840" w:w="11900" /><w:pgMar w:bottom="1440" w:footer="708" w:gutter="0" w:header="708" w:left="1800" w:right="1800" w:top="1440" /><w:cols w:space="708" /><w:docGrid w:linePitch="360" /></w:sectPr></w:body></w:document>')  # noqa

    def test_merge_rows(self):
        self.assertEqual(self.document.get_merge_fields(),
                         {'student_name', 'study_name', 'class_name', 'class_code', 'class_grade', 'thesis_grade'})

        self.document.merge(
            student_name='Bouke Haarsma',
            study='Industrial Engineering and Management',
            thesis_grade='A',
        )

        self.document.merge_rows('class_code', [
            {'class_code': 'ECON101', 'class_name': 'Economics 101', 'class_grade': 'A'},
            {'class_code': 'ECONADV', 'class_name': 'Economics Advanced', 'class_grade': 'B'},
            {'class_code': 'OPRES', 'class_name': 'Operations Research', 'class_grade': 'A'},
        ])

        with tempfile.TemporaryFile() as outfile:
            self.document.write(outfile)

        self.assert_equal_tree(self.expected_tree,
                               list(self.document.parts.values())[0].getroot())

    def test_merge_rows_no_table(self):
        """
        Merging of rows should not fail if there is no table with the given
        column in the document
        """
        self.document.merge(
            student_name='Bouke Haarsma',
            study='Industrial Engineering and Management',
            thesis_grade='A',
            class_code=[
                {'class_code': 'ECON101', 'class_name': 'Economics 101', 'class_grade': 'A'},
                {'class_code': 'ECONADV', 'class_name': 'Economics Advanced', 'class_grade': 'B'},
                {'class_code': 'OPRES', 'class_name': 'Operations Research', 'class_grade': 'A'},
            ],
            no_table=[
                {'no_table': 'Table not available'}
            ]
        )

        with tempfile.TemporaryFile() as outfile:
            self.document.write(outfile)

        self.assert_equal_tree(self.expected_tree,
                               list(self.document.parts.values())[0].getroot())

    def test_merge_rows_remove_table(self):
        """
        When flag is set and there is no data in a table, table needs to be removed
        """
        self.document.remove_empty_tables = True
        self.document.merge(
            student_name='Bouke Haarsma',
            study='Industrial Engineering and Management',
            thesis_grade='A',
            class_code=[]
        )

        with tempfile.TemporaryFile() as outfile:
            self.document.write(outfile)
        self.assertIsNone(
            list(self.document.parts.values())[0].getroot().find('.//{%(w)s}tbl' % NAMESPACES)
        )

    def test_merge_unified(self):
        self.document.merge(
            student_name='Bouke Haarsma',
            study='Industrial Engineering and Management',
            thesis_grade='A',
            class_code=[
                {'class_code': 'ECON101', 'class_name': 'Economics 101', 'class_grade': 'A'},
                {'class_code': 'ECONADV', 'class_name': 'Economics Advanced', 'class_grade': 'B'},
                {'class_code': 'OPRES', 'class_name': 'Operations Research', 'class_grade': 'A'},
            ]
        )

        with tempfile.TemporaryFile() as outfile:
            self.document.write(outfile)

        self.assert_equal_tree(self.expected_tree,
                               list(self.document.parts.values())[0].getroot())

    def tearDown(self):
        self.document.close()
    def main(self):
        list_columns = ['Global ID', 'Employee Name', 'Status', 'Notes']
        df_list = pd.DataFrame(columns=list_columns)
        self.read_files()

        for n_idx, n_row in self.df_data.iterrows():
            if len(str(n_row['Target Move Date'])) > 0:
                if n_row['Template'] == 'fix':
                    lv_template = self.template_fix
                else:
                    lv_template = self.template
                document_1 = MailMerge(self.path + lv_template)
                # print(document_1.get_merge_fields())
                # isinstance(['Target Move Date'], datetime):

                try:
                    lv_target_cn = n_row['Target Move Date'].strftime(
                        "%Y年%m月%d日")
                    lv_target_en = n_row['Target Move Date'].strftime(
                        "%d/%m/%Y")
                    lv_start_en = n_row['Start date of ZF Service'].strftime(
                        "%d/%m/%Y")
                    lv_start_cn = n_row['Start date of ZF Service'].strftime(
                        "%Y年%m月%d日")
                    if n_row['Template'] == 'fix':
                        lv_contract_end = ''
                        lv_contract_end_cn = ''
                    else:
                        lv_contract_end = n_row[
                            'Latest Contract ending date'].strftime("%d/%m/%Y")
                        lv_contract_end_cn = n_row[
                            'Latest Contract ending date'].strftime(
                                "%Y年%m月%d日")
                        print(lv_contract_end_cn)
                        # lv_contract_end = n_row['Latest Contract ending date'].to_timestamp().strftime("%d/%m/%Y")
                        # lv_date = time.localtime(time.mktime(time.strptime(n_row['Latest Contract ending date'],'%Y-%m-%d')))
                        # # print(lv_date)
                        # # lv_contract_end = time.strptime("%d/%m/%Y %H:%M:%S",lv_date)
                        # # print(lv_contract_end)
                        # # lv_contract_end_cn = n_row['Latest Contract ending date'].to_timestamp().strftime("%Y年%m月%d日")
                        # # print(lv_contract_end_cn)
                        # lv_contract_end_cn = '至' + str(lv_date.tm_year) +'年' + str(lv_date.tm_mon) + '月' + str(lv_date.tm_mday) + '日止'
                        # lv_contract_end = 'lasts until ' + str(lv_date.tm_mday) + '/' + str(lv_date.tm_mon) + '/' + str(lv_date.tm_year)

                    dict = {
                        "Chinese_Name": str(n_row['Chinese Name']),
                        "Employee_Name": str(n_row['Employee Name']),
                        "National_ID": str(n_row['National ID']),
                        #                    "Start_date_of_ZF_Service": str(n_row['Start date of ZF Service']),
                        #                    "Target_Move_Date": str(n_row['Target Move Date'])
                        "Target_Move_Date": lv_target_en,
                        "Target_Move_Date_CN": lv_target_cn,
                        "Start_date_of_ZF_Service": lv_start_en,
                        "Start_date_of_ZF_Service_CN": lv_start_cn,
                        "Contract_End_CN": lv_contract_end_cn,
                        "Contract_End": lv_contract_end,
                    }
                    document_1.merge(**dict)

                    file_path = self.path + 'xlsx/'
                    if not os.path.exists(file_path):
                        os.makedirs(file_path)

                    lv_name = self.prefix + n_row[
                        'Division for list'] + '_' + n_row['Employee Name']
                    file_docx = self.path + 'xlsx/' + lv_name + '.docx'
                    document_1.write(file_docx)
                    # time.sleep(1)

                    file_path = self.path + 'pdf' + '/' + n_row['HR BP2'] + '/'
                    if not os.path.exists(file_path):
                        os.makedirs(file_path)

                    if n_row['Mark'] != "":
                        file_path = file_path + n_row['Mark'] + '/'
                        if not os.path.exists(file_path):
                            os.makedirs(file_path)

                    #                file_pdf = file_path + 'pdf/' + lv_name + '.pdf'
                    file_pdf = file_path + lv_name + '.pdf'
                    # file_pdf = self.path + 'pdfs/' + n_row['姓名'] + '.pdf'
                    # self.generate_pdf(file_docx,file_pdf)
                    convert(file_docx, file_pdf, keep_active=True)
                    print('Success:', n_row['Employee Name'], n_idx + 1)
                    document_1.close()

                    emp_line = [
                        n_row['Globle ID'], n_row['Employee Name'], 'S', ''
                    ]
                    df_list = df_list.append(pd.Series(emp_line,
                                                       index=list_columns),
                                             ignore_index=True)

                except Exception as e:
                    print('Failed to generate file for line ', n_idx + 1, e)
                    emp_line = [
                        n_row['Globle ID'], n_row['Employee Name'], 'E', ''
                    ]
                    df_list = df_list.append(pd.Series(emp_line,
                                                       index=list_columns),
                                             ignore_index=True)
        df_list.to_csv(self.path + 'log.csv')