C
C
CheXnik2021-06-17 21:52:01
Python
CheXnik, 2021-06-17 21:52:01

Why is Docxtpl saving the wrong data?

The essence of the code is to take data from excel, and write it to word. It does everything right, but the data is still stored incorrectly.
Here is the full code:

import os
from docxtpl import DocxTemplate
from openpyxl import load_workbook

file = load_workbook(os.path.join(os.getcwd(), 'test.xlsx'))
data = file['Лист2']

doc = DocxTemplate("test.docx")

number = 2
count = 0

while count < 4:
    value = data[f'C{number}'].value
    if value:
        count = 0
        context = { 'kod_1' : str(data[f'A{number}'].value),
                    'kod_2' : str(data[f'B{number}'].value),
                    'last_name_ua': data[f'D{number}'].value,
                    'first_name_ua': data[f'E{number}'].value,
                    'last_name_en': data[f'F{number}'].value,
                    'first_name_en': data[f'G{number}'].value,
                    'date_of_birth': (str(data[f'H{number}'].value).replace('-', '/'))[:10],
                    'kod_3': str(data[f'C{number}'].value)}
        doc.render(context)
        doc.save(f"diplom/{data[f'D{number}'].value} {data[f'E{number}'].value}.docx")

        print('Сделанно дипломов >>', (number-1), ', название: ',
              f"{data[f'D{number}'].value} {data[f'E{number}'].value}.docx")

        number += 1
        count += 1

print('Работа выполнена')


it is the fields kod_1, kod_2, kod_3 that are filled with the wrong values, although during debugging the correct values ​​are in the context

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
CheXnik, 2021-06-18
@CheXnik

I decided. Pay attention to this line: it must be inside the loop, and then everything works as it should
doc = DocxTemplate("test.docx")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question