代码
def new_template(sections, task_id):
file_path = os.path.join(settings.UPLOAD_PATH, f"{task_id}.docx")
# 设置 Aspose.Words 许可证
lic = aw.License()
lic_path = os.path.join(settings.BASE_PATH, "core/Aspose.Total.Product.Family.lic")
lic.set_license(lic_path)
# 创建一个新的文档
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# 遍历节(section)
for section in sections:
if section["type"] == 'title':
# 设置标题样式
level=section.get('level',1)
builder.paragraph_format.style_identifier = getattr(
aw.StyleIdentifier, f"HEADING{level}"
)
# 添加标题内容
title = section.get('contents', '') # 获取标题内容
builder.writeln(title) # 写入标题到文档
elif section["type"] == 'text':
# 添加文本内容
text_content = section.get('contents', '') # 获取文本内容
builder.paragraph_format.style_identifier = aw.StyleIdentifier.NORMAL
builder.writeln(text_content) # 写入文本到文档
elif section["type"] == 'table':
# 添加表格内容
table_data = section.get('contents', {}) # 获取表格内容
table = builder.start_table() # 开始一个新表格
# 遍历表格行
for row_data in table_data.get('content', []):
table_row = builder.insert_cell() # 插入新的表格行
# 遍历表格单元格
for cell_data in row_data.get('content', []):
cell_content = '' # 单元格内容初始化为空字符串
if isinstance(cell_data, dict): # 检查单元格内容是否为字典类型
paragraph_content = cell_data.get('content', [])
# 遍历段落内容
for paragraph in paragraph_content:
text = paragraph.get('text', '') # 获取段落文本内容
cell_content += text # 将段落内容添加到单元格内容中
else:
cell_content = cell_data # 如果不是字典类型,则直接使用内容
colspan = cell_data.get('attrs', {}).get('colspan', 1) # 获取列合并数
rowspan = cell_data.get('attrs', {}).get('rowspan', 1) # 获取行合并数
# 合并单元格
if colspan > 1:
builder.cell_format.horizontal_merge = aw.tables.CellMerge.FIRST # 水平合并第一个单元格
if rowspan > 1:
builder.cell_format.vertical_merge = aw.tables.CellMerge.FIRST # 垂直合并第一个单元格
table_row.cell_format.wrap_text = True # 设置单元格内容自动换行
table_row.cell_format.vertical_alignment = aw.tables.CellVerticalAlignment.CENTER # 设置单元格内容垂直居中
builder.write(cell_content) # 写入单元格内容
builder.end_table() # 结束表格
# 保存文档
doc.save(file_path)
return file_path
new_template(sections, '11111hhhhh')
传入的信息
sections = [{
"type": "title", "level": 1, "contents": "1、你好"
},
{
"type": "text", "contents": "哈哈哈哈哈"
},
{
"type": "table", "contents": {
"type": "table",
"attrs": {
"id": "ebf4362db15f18b8&&dbda60e3b9f8d503"
},
"content": [
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "机构\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "负责人\u0007"
}
]
}
]
}
]
},
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 4,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "研究中心\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 2,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "广东省人民医院\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "主要研究者:吴一龙\u0007"
}
]
}
]
}
]
},
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "主要研究者: 杨衿记\u0007"
}
]
}
]
}
]
},
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "浙江大学附属第一医院\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "主要研究者:周建英\u0007"
}
]
}
]
}
]
},
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "湖南省肿瘤医院\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "主要研究者:杨农\u0007"
}
]
}
]
}
]
},
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "申办方\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "江苏万邦生化医药集团有限责任公司\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "项目负责人:周永春\u0007"
}
]
}
]
}
]
},
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "合同研究组织\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "北京复星医药科技开发有限公司\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "项目经理:程艳秋\u0007"
}
]
}
]
}
]
},
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "生物检测单位\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "广东省人民医院I期研究室\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "项目负责人:王曦培\u0007"
}
]
}
]
}
]
},
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "数据管理单位\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "北京复星医药科技开发有限公司\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "数据管理经理:蔡红霞\u0007"
}
]
}
]
}
]
},
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "统计单位\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "北京复星医药科技开发有限公司\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "生物统计经理:周艳玲\u0007"
}
]
}
]
}
]
},
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "中心实验室\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "昆皓睿诚医药研发(北京)有限公司\u0007"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"colspan": 1,
"rowspan": 1,
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "项目经理:辜朝霞\u0007"
}
]
}
]
}
]
}
]
}
},
{
"type": "title", "leve": 1, "contents": "哈哈哈哈"
},
]