elapouya / python-docx-template

Use a docx as a jinja2 template
GNU Lesser General Public License v2.1
1.91k stars 378 forks source link

vm problem, anyone can help? #456

Open TangLeeee opened 1 year ago

TangLeeee commented 1 year ago

here's my code

from docxtpl import DocxTemplate, RichText

tpl = DocxTemplate('template/test.docx')

context = {
    'km': {'city': 918.28, 'countryside': 125.32, 'highway': 325.21, 'total': 1368.81},
    'pic_num_time': {'dawn': 0, 'daytime': 7916, 'evening': 384, 'night': 9},
    'pic_num_weather': {'sunny': 7194, 'cloudy': 823, 'rainy': 117, 'snowy': 175, 'foggy': 0},
    'OD_label_dis': [
        {'type': 'Vehicle', 'sub_type': 'bus', 'GT_label_num': 3061, 'PRED_label_num': 2384},
        {'type': 'Vehicle', 'sub_type': 'car', 'GT_label_num': 35039, 'PRED_label_num': 29529},
        {'type': 'Vehicle', 'sub_type': 'dont_car', 'GT_label_num': 23169, 'PRED_label_num': 0},
        {'type': 'Vehicle', 'sub_type': 'minicar', 'GT_label_num': 150, 'PRED_label_num': 0},
        {'type': 'Vehicle', 'sub_type': 'pickup_truck', 'GT_label_num': 419, 'PRED_label_num': 0},
        {'type': 'Vehicle', 'sub_type': 'special', 'GT_label_num': 1493, 'PRED_label_num': 1035},
        {'type': 'Vehicle', 'sub_type': 'suv', 'GT_label_num': 18516, 'PRED_label_num': 14307},
        {'type': 'Vehicle', 'sub_type': 'truck_trailer', 'GT_label_num': 36, 'PRED_label_num': 0},
        {'type': 'Vehicle', 'sub_type': 'truck', 'GT_label_num': 7079, 'PRED_label_num': 5025},
        {'type': 'Vehicle', 'sub_type': 'van', 'GT_label_num': 7523, 'PRED_label_num': 5875},
        {'type': 'Others', 'sub_type': 'animal', 'GT_label_num': 28, 'PRED_label_num': 0},
        {'type': 'Others', 'sub_type': 'bicycle', 'GT_label_num': 2006, 'PRED_label_num': 1869},
        {'type': 'Others', 'sub_type': 'cyclist', 'GT_label_num': 5507, 'PRED_label_num': 5012},
        {'type': 'Others', 'sub_type': 'dont_car', 'GT_label_num': 6584, 'PRED_label_num': 0},
        {'type': 'Others', 'sub_type': 'info_signs', 'GT_label_num': 31975, 'PRED_label_num': 21653},
        {'type': 'Others', 'sub_type': 'motorcycle', 'GT_label_num': 6405, 'PRED_label_num': 6018},
        {'type': 'Others', 'sub_type': 'o_pole_ignore', 'GT_label_num': 45567, 'PRED_label_num': 0},
        {'type': 'Others', 'sub_type': 'pedestrian', 'GT_label_num': 12630, 'PRED_label_num': 8779},
        {'type': 'Others', 'sub_type': 'traffic_signs', 'GT_label_num': 29840, 'PRED_label_num': 19367},
        {'type': 'Others', 'sub_type': 'tricycle', 'GT_label_num': 2878, 'PRED_label_num': 2186},
    ],
}

tpl.render(context)
tpl.save('output/test.docx')

template as: image get the below result: 图片1 obviously it does't not work properly, i expect 'vehicle' and 'Others' merged as two different lines, but finally only got 'vehicle' , or is there any solutions?

test.docx

tsy19900929 commented 1 year ago

https://github.com/tsy19900929/docxtpl_dynamic_vertical_merging test1