I'm building an app that takes text from xml file then show it in PySide6 QWidgetTable and finally write it to a python-docxtpl (docx file). Everything works fine until Data is written to the docx file. QtableWidget cells text are are as desired. The pictures shows the original text (in xml and QtableWidget) and the output text in the docx file.
pictures links:
QtableWidget Picture:
Docx Picture
Code is working fine
method to read xml and fill QtableWidget
def fill_teachers_tables(self):
tree = ET.parse(self.teachers_xml)
root = tree.getroot()
teacher = self.teachers_dialog.list_widget.currentItem().text()
hours = [hour.get('name') for hour in root.xpath('Teacher')[0][0]]
hours_list = [hour.get('name') for hour in root.xpath(
f'Teacher[@name="{teacher}"]/Day/Hour')]
self.teachers_dialog.morning_table.setColumnCount(
len(self.morning_days))
self.teachers_dialog.morning_table.setRowCount(len(hours))
self.teachers_dialog.morning_table.setHorizontalHeaderLabels(
self.morning_days)
self.teachers_dialog.morning_table.setVerticalHeaderLabels(hours)
for i in range(len(self.morning_days)):
for j in range(len(hours_list)):
try:
self.teachers_dialog.morning_table.setItem(
j, i, QTableWidgetItem(
root.xpath(
f'Teacher[@name="{teacher}"]/Day[@name="{self.morning_days[i]}"]/Hour[@name="{hours[j]}"]/Students')[0]
.get('name')))
except IndexError:
self.teachers_dialog.morning_table.setItem(
j, i, QTableWidgetItem(''))
method to write to docx template:
def export_single(self):
teacher = self.teachers_dialog.list_widget.currentItem().text()
tpl = DocxTemplate('./resources/template.docx')
context = {
'teacher': '',
'days': [],
'tbl_contents': [],
'hours': 0,
}
context['teacher'] = teacher
m_row_headers = [self.teachers_dialog.morning_table.verticalHeaderItem(
i).text() for i in range(self.teachers_dialog.morning_table.rowCount())]
m_col_headers = [self.teachers_dialog.morning_table.horizontalHeaderItem(i).text(
).split(' ')[0] for i in range(self.teachers_dialog.morning_table.columnCount())]
for i in range(len(m_row_headers)):
context['tbl_contents'].append({'hour': '', 'sets': []})
for j in range(len(m_col_headers)):
item = self.teachers_dialog.morning_table.item(i, j)
if item:
context['hours'] += 1
context['tbl_contents'][i]['hour'] = f'{m_row_headers[i]}'
context['tbl_contents'][i]['sets'].append(item.text())
else:
context['tbl_contents'][i]['hour'] = f'{m_row_headers[i]}'
context['tbl_contents'][i]['sets'].append('')
tpl.render(context)
tpl.save('result.docx')
PS: I cant't embed pictures.
I tried to use both python-bidi and arabic-reshaper but didn't work.
I think the problem is with the period (.) and the hyphen (-) I experienced that all Arabic text ended with period (.), the period goes to start of text.
I found the problem.
The problem is with docxtpl.
you need to edit all the paragraphs font style right to left in the template file.
so did:
tpl = DocxTemplate('./resources/template.docx')
for p in tpl.docx.paragraphs:
p.style.font.rtl = True
for table in tpl.docx.tables:
for i in range(len(table.columns)):
for j in range(len(table.rows)):
for p in table.cell(j, i).paragraphs:
p.style.font.rtl = True
# Then fill your context and render it to the template