Tiaohh
1
async def generate_docx_with_result_laikai(
header_list: list,
table_list: list,
save_path,
template_path,
):
logger.info(f"header list内容: {header_list}")
logger.info(f"table list内容: {table_list}")
base_file = template_path
clear_save_path = base_file
doc_main = aw.Document(clear_save_path)
paragraphs = doc_main.get_child_nodes(aw.NodeType.PARAGRAPH, True)
for para in paragraphs:
para = para.as_paragraph()
para_content = para.to_string(aw.SaveFormat.TEXT)
para_content = para_content.replace("\r", "")
para_content = (
para_content.strip()
) # 特殊地方,发现目录中有这个符号,暂时不知道符号是干啥的
if (
para_content in header_list or para_content.capitalize() in header_list
): # 如果当前段落中有写作内容,那么找到内容,找到生成的结果
table_header = aw.Paragraph(doc_main)
table_header.paragraph_format.style_identifier = aw.StyleIdentifier.NORMAL
table_header.paragraph_format.alignment = aw.ParagraphAlignment.CENTER
para = find_next_heading_after_target(paragraphs, para_content)
run = aw.Run(doc_main, "")
# 将文本设置为加粗
run.font.bold = True
# 将加粗的 Run 对象添加到表头段落中
table_header.append_child(run)
# 增加一步 现将当前para段落中的内容清空
para.parent_node.insert_after(table_header, para)
logger.info(f"当前段落内容:{para_content}")
try:
if para_content in ["APPENDICES", "REFERENCES"]:
para_content = para_content.capitalize()
idx_num = header_list.index(para_content)
# 获取header对应的table
num_tables = len(table_list[idx_num])
logger.info(f"当前表格长度:{num_tables}")
for _index, info in enumerate(table_list[idx_num]):
table_id = info.get("id", "")
is_header_and_footer = info.get("is_header_and_footer", "")
logger.warning(f"table_id in table list:{table_id}")
aw.Document(
os.path.join(settings.UPLOAD_PATH, f"{table_id}.rtf")
).save(os.path.join(settings.UPLOAD_PATH, f"{table_id}.docx"))
# 判断是否需要页眉页脚 False为删除页眉页脚
if not is_header_and_footer:
pass
document = Document(
os.path.join(settings.UPLOAD_PATH, f"{table_id}.docx")
)
for section in document.sections:
section.footer.is_linked_to_previous = True
section.header.is_linked_to_previous = True
document.save(
os.path.join(settings.UPLOAD_PATH, f"{table_id}.docx")
)
doc_rtf = aw.Document(
os.path.join(settings.UPLOAD_PATH, f"{table_id}.docx")
)
builder = aw.DocumentBuilder(doc_rtf)
# 判断分页是否续表
builder.row_format.heading_format = True
doc_rtf.save(os.path.join(settings.UPLOAD_PATH, f"{table_id}.docx"))
_doc = aw.Document(
os.path.join(settings.UPLOAD_PATH, f"{table_id}.docx")
)
_tables = _doc.get_child_nodes(aw.NodeType.TABLE, True)
_tables = [t for t in _tables]
for table in _tables:
if table.get_ancestor(aw.NodeType.BODY):
i = _tables.index(table)
temp = _tables[i]
_tables[i] = _tables[i - 1]
_tables[i - 1] = temp
# 删除表格第一行内容
logger.info(f"插入表格: {table_id}")
for (
table
) in _tables: # 将table 信息反向的插入到word文件中。TODO 表格美化
table_clone = table.as_table().clone(True)
imported_table = doc_main.import_node(table_clone, True)
logger.info(
f"imported table 节点类型: {imported_table.node_type}"
)
if imported_table.node_type == aw.NodeType.TABLE:
logger.info(f"imported table 节点是表格")
imported_table = imported_table.as_table()
# imported_table.first_row.remove()
imported_table.preferred_width = (
aw.tables.PreferredWidth.from_percent(100)
)
# if fooots and headers:
# if imported_table.first_row:
# # 克隆第一行
# cloned_row = imported_table.first_row.clone(True)
# row = cloned_row.as_row()
# # 修改克隆的第一行的内容
# for cell in row.cells:
# cell = cell.as_cell()
# if cell.paragraphs: # 确保单元格中有段落
# first_paragraph = cell.paragraphs[
# 0
# ] # 获取第一个段落
# first_paragraph.remove_all_children() # 清除段落中的所有子节点
#
# # 创建新的 Run 并设置内容
# new_run = aw.Run(doc_main)
# new_run.text = headers[0] # 修改内容
#
# # 将新的 Run 添加到段落中
# first_paragraph.append_child(new_run)
#
# # 将克隆的第一行插入到表格的开头
# imported_table.insert_before(
# cloned_row, imported_table.first_row
# )
# # 将新行插入到表格的第一行之前
# # 添加与其他行相同数量的单元格
# if imported_table.last_row:
# # 克隆最后一行
# cloned_row = imported_table.first_row.clone(True)
# row = cloned_row.as_row()
# # 修改克隆行的内容
# for cell in row.cells:
# cell = cell.as_cell() # 确保是单元格对象
# if cell.paragraphs: # 确保单元格中有段落
# first_paragraph = cell.paragraphs[
# 0
# ] # 获取第一个段落
# first_paragraph.remove_all_children() # 清除段落中的所有子节点
#
# # 创建新的 Run 并设置内容
# new_run = aw.Run(doc_main)
# new_run.text = fooots[0] # 设置新内容
#
# # 将新的 Run 添加到段落中
# first_paragraph.append_child(new_run)
#
# # 将克隆的最后一行插入到表格的末尾
# imported_table.insert_after(
# cloned_row, imported_table.last_row
# )
for index, row in enumerate(imported_table.rows):
row = row.as_row()
# print(index, row.get_text().strip())
for cell_index, cell in enumerate(row.cells):
cell = cell.as_cell()
cell.cell_format.vertical_alignment = (
aw.tables.CellVerticalAlignment.BOTTOM
)
for paragraph in cell.paragraphs:
paragraph = paragraph.as_paragraph()
# 居中对齐
for run in paragraph.runs:
run = run.as_run()
run.font.name = (
"Courier New" # 设置西文是新罗马字体
)
run.font.name_far_east = "宋体"
run.font.size = 8
print("一个表格一件结束")
# 在插入段落标题之后插入段落内容
# insert_table_after_paragraph(doc_main, para, imported_table)
# para.parent_node.insert_before(imported_table, para)
table_header.parent_node.insert_after(
imported_table, table_header
)
if _index < num_tables - 1:
table_newline = aw.Paragraph(doc_main)
run = aw.Run(doc_main, "")
table_newline.append_child(run)
imported_table.parent_node.insert_before(
table_newline, imported_table
)
else:
logger.info(f"当前段落不需要插入表格内容")
# 插入result 模型输出结果 结果只插入一次,table插入完成后插入
# TODO 段落美化
except:
logger.warning(f"没有找到header{traceback.format_exc()}")
print("没有找到header", traceback.format_exc())
doc_main.save(save_path)
Tiaohh
2
111.docx (24.7 KB)
续表的表头也不对啊。插入下面的表 怎么是上面的表格的表头呢