Пример #1
0
    def merge_test(self):
        template = "..\\data\\Template.docx"

        document = MailMerge(template)
        print(document.get_merge_fields())

        data = {
            'cdm_total': "232",
            'made_by': "최인수",
            'KOMPSAT-2': "123",
            'asdfasd': '12321'
        }

        row = {
            'last_cdm_creation': "test",
            'CDM_NO': "test",
            'CREATION_DATE': "test",
            'PROBABILITY': "test",
            'TCA': "test",
            'EVENTNUM': "test",
            'MISS_DISTANCE': "test",
            'SAT1_NAME': "test",
            'SAT1_NORAD': "test",
        }

        rows = [row, row]

        print(type(data))
        document.merge_rows('CDM_NO', rows)
        document.merge_templates([data], separator='page_break')

        document.write("..\\data\\result.docx")
def writeToDocx(txtFilePath, templatePath, outputPath):
    text_file = open(txtFilePath, 'r')
    newList = text_file.readlines()
    text_file.close()
    delimeted = [line.split('\t') for line in newList]
    header = delimeted[0]
    values = delimeted[1:]
    template = templatePath
    document = MailMerge(template)

    big_Dict = ([{head:val for head, val in zip(header, val)} for val in values])
    document.merge_templates(big_Dict, 'nextPage_section')
    document.write(outputPath)
Пример #3
0
def write2one_docx(data, template, output, separator='page_break'):
    """
    根据data数组生成一个len(data)个的一个总文档
    :param data: 数组,键值对用来替换模板
    :param template: 模板完整名称
    :param output: 输出文件名称
    :param separator: 分隔符 默认为换页符
    :return:
    """
    doc = MailMerge(template)
    doc.merge_templates(data, separator=separator)

    doc.write(output)
Пример #4
0
    def _merge(self, file, context) -> bytes:
        document = MailMerge(file)
        output = BytesIO()

        if isinstance(context, (tuple, list)):
            # Multiple records & pages
            document.merge_templates(context, separator='continuous_section')
        else:
            # Single page
            document.merge(**context)

        document.write(output)

        return output.getvalue()
Пример #5
0
def CsvToWord():
    print("Starting Card Filler...")

    #Read cards from csv
    with open(sheetname, newline='') as csvfile:
        spamreader = csv.reader(csvfile, dialect='excel')
        singleformdata = []
        carddata = []
        i = 1
        print("Reading CSV Data...")
        for row in spamreader:
            #For the first card, reset the form data storage object
            if i == 1:
                singleformdata = {
                    'Card' + str(i) + 'Name': str(row[0]),
                    'Card' + str(i) + 'Mana': str(row[1]),
                    'Card' + str(i) + 'Type': str(row[2]),
                    'Card' + str(i) + 'MagicType': str(row[3]),
                    'Card' + str(i) + 'Text': str(row[4]),
                }
            #For each subsequent card, add the data to the form data
            else:
                singleformdata['Card' + str(i) + 'Name'] = str(row[0])
                singleformdata['Card' + str(i) + 'Mana'] = str(row[1])
                singleformdata['Card' + str(i) + 'Type'] = str(row[2])
                singleformdata['Card' + str(i) + 'MagicType'] = str(row[3])
                singleformdata['Card' + str(i) + 'Text'] = str(row[4])
            i += 1
            if i > numCardsPerForm:
                i = 1
                carddata.append(singleformdata)

    #Fill the final sheet with blank cards if there the number of cards isn't divisible by four
    if i != 1:
        while i <= numCardsPerForm:
            singleformdata['Card' + str(i) + 'Name'] = ''
            singleformdata['Card' + str(i) + 'Mana'] = ''
            singleformdata['Card' + str(i) + 'Type'] = ''
            singleformdata['Card' + str(i) + 'MagicType'] = ''
            singleformdata['Card' + str(i) + 'Text'] = ''
            i += 1
        carddata.append(singleformdata)

    #Create new document
    document = MailMerge(docname)
    print("Creating Document...")
    document.merge_templates(carddata, 'page_break')
    document.write(outputDocName)
    print("Cards Sucessfully Exported to " + outputDocName)
Пример #6
0
class MergeReport:
    document = None

    def merge_init(self):
        template = iface.LOC_REPORT_TEMPLATE
        self.document = MailMerge(template)

    def merge_table_data(self, anchor, data):
        self.document.merge_rows(anchor, data)

    def merge_plain_data(self, data):
        self.document.merge_templates([data], separator='page_break')

    def merge_create_report(self, fname):
        self.document.write(iface.LOC_DAILY_REPORT.format(fname))

    def merge_test(self):
        template = "..\\data\\Template.docx"

        document = MailMerge(template)
        print(document.get_merge_fields())

        data = {
            'cdm_total': "232",
            'made_by': "최인수",
            'KOMPSAT-2': "123",
            'asdfasd': '12321'
        }

        row = {
            'last_cdm_creation': "test",
            'CDM_NO': "test",
            'CREATION_DATE': "test",
            'PROBABILITY': "test",
            'TCA': "test",
            'EVENTNUM': "test",
            'MISS_DISTANCE': "test",
            'SAT1_NAME': "test",
            'SAT1_NORAD': "test",
        }

        rows = [row, row]

        print(type(data))
        document.merge_rows('CDM_NO', rows)
        document.merge_templates([data], separator='page_break')

        document.write("..\\data\\result.docx")
Пример #7
0
                merge_match_results.append(str_surcharge_part)
                list_value = list_value + 1
                conc_over = 0
                surcharge_part = 0
                load_over = 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