elapouya / python-docx-template

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

{%vm%} #521

Open hayratjan opened 10 months ago

hayratjan commented 10 months ago
from docxtpl import DocxTemplate

# 初始化模板对象
tpl = DocxTemplate("./temp.docx")

# 定义字典 context 填充渲染模板的数据
context = {
    "data": [{
        "name": "1",
        "data": [{
            "name": "11",
            "data": [1, 2, 3]
        }, {
            "name": "12",
            "data": [1, 2, 3]
        }]
    }, {
        "name": "2",
        "data": [{
            "name": "21",
            "data": [1, 2, 3]
        }, {
            "name": "22",
            "data": [1, 2, 3]
        }]
    }
    ]
}

# 开始渲染context数据到模板文件中
tpl.render(context)
tpl.save("./示例结果文档.docx")

Template

image

Output

image

Expected

image
mujianfenghe commented 4 months ago
from docxtpl import DocxTemplate

# 初始化模板对象
tpl = DocxTemplate("./temp.docx")

# 定义字典 context 填充渲染模板的数据
context = {
    "data": [{
        "name": "1",
        "data": [{
            "name": "11",
            "data": [1, 2, 3]
        }, {
            "name": "12",
            "data": [1, 2, 3]
        }]
    }, {
        "name": "2",
        "data": [{
            "name": "21",
            "data": [1, 2, 3]
        }, {
            "name": "22",
            "data": [1, 2, 3]
        }]
    }
    ]
}

# 开始渲染context数据到模板文件中
tpl.render(context)
tpl.save("./示例结果文档.docx")

Template

image

Output

image

Expected

image

你好,我也遇到了同样的问题,请你解决了么?

hayratjan commented 4 months ago

最后我自己写了处理方法,使用python-docx的库处理的,最后成功处理。


def merge_cells(self, tables):
        """
        合并Word文档中的单元格
        :param tables: 要合并的表格
        """
        index_zhi_biao = False
        for table in tables[23:]:
            rows = table.rows
            old = ''
            old_ = ''
            old_text = ''
            old_text_ = ''
            for row in rows:
                cells = row.cells
                # 一级级指标
                if cells[0].text == "一级指标":
                    index_zhi_biao = True
                # if "指标" in cells[0].text:
                if index_zhi_biao:
                    if old_text and old and old_text[:4] in cells[0].text or cells[0].text == "":
                        cells[0].text = ''
                        cells[0].merge(old)
                        cells[0].text.replace('\n', '')

                    if cells[0].text[:4] not in old_text:
                        if cells[0].text.replace == "":
                            continue
                        old = cells[0]
                        old_text = cells[0].text

                    # 二级指标
                    if old_text_ and old_ and old_text_[:4] in cells[1].text and cells[0].text[:4] in old_text or cells[
                        1].text.replace(' ', '') == "":
                        cells[1].text = ''
                        cells[1].merge(old_)
                        cells[1].text.replace('\n', '')

                    if cells[1].text not in old_text_:
                        if cells[0].text.replace == "":
                            continue
                        old_ = cells[1]
                        old_text_ = cells[1].text

            for row in rows:
                cells = row.cells
                cells[0].text = cells[0].text.replace('\n', '')
                cells[1].text = cells[1].text.replace('\n', '')
                # 居中
                paragraph = cells[0].paragraphs[0]
                paragraph.alignment = 1
                # for run in paragraph.runs:
                #     run.style.name = 'Default Paragraph Font'
                #     run.style.style_id = 'a0'

                # run.font.size = Pt(10.5)  # 设置字号为小五
                # run.font.bold = True  # 设置加粗
                # 居中
                paragraph = cells[1].paragraphs[0]
                paragraph.alignment = 1
# from docxtpl import DocxTemplate  #docx-template
# self.tpl = DocxTemplate("Budget/Download/report.docx")
# self.merge_cells(self.tpl.docx.tables)

模板

image

处理后显示

image

不懂加我微信咖啡钱,让你学会,微信号:hayratjan-0998

hayratjan commented 4 months ago
from docxtpl import DocxTemplate

# 初始化模板对象
tpl = DocxTemplate("./temp.docx")

# 定义字典 context 填充渲染模板的数据
context = {
    "data": [{
        "name": "1",
        "data": [{
            "name": "11",
            "data": [1, 2, 3]
        }, {
            "name": "12",
            "data": [1, 2, 3]
        }]
    }, {
        "name": "2",
        "data": [{
            "name": "21",
            "data": [1, 2, 3]
        }, {
            "name": "22",
            "data": [1, 2, 3]
        }]
    }
    ]
}

# 开始渲染context数据到模板文件中
tpl.render(context)
tpl.save("./示例结果文档.docx")

模板

图像

输出

图像

预期

图像

你好,我也遇到了同样的问题,请你解决什么问题?

可以加我微信,hayratjan-0998

mujianfenghe commented 4 months ago
from docxtpl import DocxTemplate

# 初始化模板对象
tpl = DocxTemplate("./temp.docx")

# 定义字典 context 填充渲染模板的数据
context = {
    "data": [{
        "name": "1",
        "data": [{
            "name": "11",
            "data": [1, 2, 3]
        }, {
            "name": "12",
            "data": [1, 2, 3]
        }]
    }, {
        "name": "2",
        "data": [{
            "name": "21",
            "data": [1, 2, 3]
        }, {
            "name": "22",
            "data": [1, 2, 3]
        }]
    }
    ]
}

# 开始渲染context数据到模板文件中
tpl.render(context)
tpl.save("./示例结果文档.docx")

模板

图像

输出

图像

预期

图像

你好,我也遇到了同样的问题,请你解决什么问题?

可以加我微信,hayratjan-0998

from docxtpl import DocxTemplate

# 初始化模板对象
tpl = DocxTemplate("./temp.docx")

# 定义字典 context 填充渲染模板的数据
context = {
    "data": [{
        "name": "1",
        "data": [{
            "name": "11",
            "data": [1, 2, 3]
        }, {
            "name": "12",
            "data": [1, 2, 3]
        }]
    }, {
        "name": "2",
        "data": [{
            "name": "21",
            "data": [1, 2, 3]
        }, {
            "name": "22",
            "data": [1, 2, 3]
        }]
    }
    ]
}

# 开始渲染context数据到模板文件中
tpl.render(context)
tpl.save("./示例结果文档.docx")

模板

图像

输出

图像

预期

图像

你好,我也遇到了同样的问题,请你解决什么问题?

可以加我微信,hayratjan-0998

一定要改代码么?我不太想改代码,想利用本身的特性完成这个效果

hayratjan commented 4 months ago
from docxtpl import DocxTemplate

# 初始化模板对象
tpl = DocxTemplate("./temp.docx")

# 定义字典 context 填充渲染模板的数据
context = {
    "data": [{
        "name": "1",
        "data": [{
            "name": "11",
            "data": [1, 2, 3]
        }, {
            "name": "12",
            "data": [1, 2, 3]
        }]
    }, {
        "name": "2",
        "data": [{
            "name": "21",
            "data": [1, 2, 3]
        }, {
            "name": "22",
            "data": [1, 2, 3]
        }]
    }
    ]
}

# 开始渲染context数据到模板文件中
tpl.render(context)
tpl.save("./示例结果文档.docx")

模板

图像

输出

图像

预期

图像

你好,我也遇到了同样的问题,请你解决什么问题?

可以加我微信,hayratjan-0998

from docxtpl import DocxTemplate

# 初始化模板对象
tpl = DocxTemplate("./temp.docx")

# 定义字典 context 填充渲染模板的数据
context = {
    "data": [{
        "name": "1",
        "data": [{
            "name": "11",
            "data": [1, 2, 3]
        }, {
            "name": "12",
            "data": [1, 2, 3]
        }]
    }, {
        "name": "2",
        "data": [{
            "name": "21",
            "data": [1, 2, 3]
        }, {
            "name": "22",
            "data": [1, 2, 3]
        }]
    }
    ]
}

# 开始渲染context数据到模板文件中
tpl.render(context)
tpl.save("./示例结果文档.docx")

模板

图像

输出

图像

预期

图像

你好,我也遇到了同样的问题,请你解决什么问题?

可以加我微信,hayratjan-0998

一定要代码吗?我不太想代码,想利用它的特性完成这个效果

如果出现一个以上就要得手动写,这个我试过,真的没有办法,哈哈哈,除非你成功的修改库里面的代码,但是不建议

tsy19900929 commented 2 months ago

https://github.com/tsy19900929/docxtpl_dynamic_vertical_merging