@Tiaohh 半途而废:
@staticmethod
def find_last_related_paragraph(para):
last_related_paragraph = para
while (
last_related_paragraph.next_sibling
and last_related_paragraph.next_sibling.as_paragraph().paragraph_format.outline_level
> para.paragraph_format.outline_level
):
last_related_paragraph = last_related_paragraph.next_sibling
return last_related_paragraph
def test_generate_docx_with_result_laikai(self):
header_list = ["REFERENCES"]
doc_main = aw.Document(MY_DIR + "111 (1).docx")
builder_main = aw.DocumentBuilder(doc_main)
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段落中的内容清空
# para.parent_node.insert_after(table_header, para)
if para_content in ["APPENDICES", "REFERENCES"]:
# Create a section break
para = self.find_last_related_paragraph(para)
previous_para = para
while previous_para is not None:
if previous_para.node_type is aw.NodeType.PARAGRAPH and "C-Heading" in previous_para.as_paragraph().paragraph_format.style_name:
previous_para = previous_para
break
previous_para = previous_para.previous_pre_order(doc_main)
previous_para = previous_para.previous_sibling
while previous_para is not None:
if previous_para.node_type is aw.NodeType.PARAGRAPH:
previous_para = previous_para
break
previous_para = previous_para.previous_pre_order(doc_main)
builder_main.move_to(previous_para)
builder_main.insert_paragraph()
builder_main.paragraph_format.clear_formatting()
builder_main.list_format.list = None
builder_main.insert_break(aw.BreakType.SECTION_BREAK_CONTINUOUS)
# run = aw.Run(doc_main, "")
# # 将文本设置为加粗
# run.font.bold = True
# # 将加粗的 Run 对象添加到表头段落中
# table_header.append_child(run)
# idx_num = header_list.index(para_content)
# # 获取header对应的table
#
# num_tables = len(table_list[idx_num][::-1])
# for _index, info in enumerate(table_list[idx_num]):
# table_id = info.get("id", "")
# is_header_and_footer = info.get("is_header_and_footer", "")
# 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:
# 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(MY_DIR + "t_ae_1(1).docx")
builder = aw.DocumentBuilder(doc_rtf)
builder.row_format.heading_format = False
doc_rtf.save(ARTIFACTS_DIR + "t_ae_1(1).docx")
_doc = aw.Document(ARTIFACTS_DIR + "t_ae_1(1).docx")
_tables = _doc.get_child_nodes(aw.NodeType.TABLE, True)
_tables = [t for t in _tables]
for table in _tables[::-1]:
if table.get_ancestor(aw.NodeType.BODY):
i = _tables.index(table)
temp = _tables[i]
_tables[i] = _tables[i - 1]
_tables[i - 1] = temp
all_tables_count = sum(p.parent_node.node_type == aw.NodeType.BODY for p in _tables)
curr_count = 0
for table in _tables[::-1]: # 将table 信息反向的插入到word文件中。TODO 表格美化
if table.get_ancestor(aw.NodeType.BODY):
table_clone = table.as_table().clone(True)
imported_table = doc_main.import_node(table_clone, True)
imported_table.as_table().preferred_width = aw.tables.PreferredWidth.from_percent(100)
para = para.parent_node.insert_after(imported_table, para)
para = para.as_table().parent_node.insert_after(aw.Paragraph(doc_main), para)
if curr_count < all_tables_count:
builder_main.move_to(para)
builder_main.insert_break(aw.BreakType.SECTION_BREAK_NEW_PAGE)
para = builder_main.current_paragraph
curr_count += 1
# if imported_table.node_type == aw.NodeType.TABLE:
# imported_table = imported_table.as_table()
# imported_table.preferred_width = (
# aw.tables.PreferredWidth.from_percent(100)
# )
#
# 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
# table_newline = aw.Paragraph(doc_main)
# run = aw.Run(doc_main, "")
# table_newline.append_child(run)
# imported_table.parent_node.insert_after(
# table_newline, imported_table
# )
doc_main.save(ARTIFACTS_DIR + "Result.docx")
Result.docx (26.5 KB)
现在,您需要在第 2 节和第 3 节中添加页眉/页脚,并使用我上面分享的代码禁用 is_link_to_previous。
这是您需要的正确结果吗?