怎么在表格上方设置标题内容呢
for table in _tables[::-1]: # 将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.preferred_width = aw.tables.PreferredWidth.from_percent(100)
for index, row in enumerate(imported_table.rows):
row = row.as_row()
for cell in row.cells:
cell = cell.as_cell()
for paragraph in cell.paragraphs:
paragraph = paragraph.as_paragraph()
# 居中对齐
_para_content = paragraph.to_string(aw.SaveFormat.TEXT)
try:
if _para_content.strip()[0] == "表":
logger.info(f"当前内容 {_para_content} 是表头,使用特殊格式")
paragraph.paragraph_format.alignment = aw.ParagraphAlignment.CENTER
else:
paragraph.paragraph_format.alignment = aw.ParagraphAlignment.LEFT
except:
pass
for run in paragraph.runs:
run = run.as_run()
run.font.name = "Times New Roman" # 设置西文是新罗马字体
run.font.name_far_east = "宋体"
run.font.size = 8
# run.font.bold = True
para.parent_node.insert_after(imported_table, para)
可以在这个插入表格之前给我插入一个标题吗
可以看到我的问题嘛??????
@Tiaohh 您可以使用以下代码:
# Create header paragraph
table_header = aw.Paragraph(doc)
table_header.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING1
table_header.append_child(aw.Run(doc, "Table Header"))
# Insert it into the document
para.parent_node.insert_after(table_header, para)
# Insert the table after the header paragraph
table_header.parent_node.insert_after(imported_table, table_header)
需要在表格循环里面插入吗
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: # 如果当前段落中有写作内容,那么找到内容,找到生成的结果
# 增加一步 现将当前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
if res.get(para_content, ''):
for table_id in table_list[idx_num]:
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"))
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 = False
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]
logger.info(f"插入表格: {table_id}")
for table in _tables[::-1]: # 将table 信息反向的插入到word文件中。TODO 表格美化
table_header = aw.Paragraph()
table_header.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING1
table_header.append_child(aw.Run(doc_main, "Table Header"))
para.parent_node.insert_after(table_header, para)
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.preferred_width = aw.tables.PreferredWidth.from_percent(100)
for index, row in enumerate(imported_table.rows):
row = row.as_row()
for cell in row.cells:
cell = cell.as_cell()
for paragraph in cell.paragraphs:
paragraph = paragraph.as_paragraph()
# 居中对齐
_para_content = paragraph.to_string(aw.SaveFormat.TEXT)
try:
if _para_content.strip()[0] == "表":
logger.info(f"当前内容 {_para_content} 是表头,使用特殊格式")
paragraph.paragraph_format.alignment = aw.ParagraphAlignment.CENTER
else:
paragraph.paragraph_format.alignment = aw.ParagraphAlignment.LEFT
except:
pass
for run in paragraph.runs:
run = run.as_run()
run.font.name = "Times New Roman" # 设置西文是新罗马字体
run.font.name_far_east = "宋体"
run.font.size = 8
# run.font.bold = True
table_header.parent_node.insert_after(imported_table, para)
else:
logger.warning(f"imported table 节点类型: {imported_table.node_type}")为什么不对呀
table_header = aw.Paragraph()这里报错
为什么写入的内容是标题1呢 不能设置成正文吗
@Tiaohh 您应该将 Document
实例传递到 Paragraph
构造函数中:
table_header = aw.Paragraph(doc)
您可以按照需要设置段落格式。 提供的代码简单地展示了基本技术。
但是插入的内容为什么第一个表格没插入进去标题呢? 怎么感觉像下标+1了
您可以看下这个图片? 第一个标题没有插入表的上面
好的,那我插入表格数据怎么不插入表格第一行数据呢
for table in _tables[::-1]: # 将table 信息反向的插入到word文件中。TODO 表格美化
table_header = aw.Paragraph(doc_main)
table_header.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING1
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.preferred_width = aw.tables.PreferredWidth.from_percent(100)
for index, row in enumerate(imported_table.rows):
row = row.as_row()
if index == 0:
table_header.append_child(aw.Run(doc_main, row.get_text()))
para.parent_node.insert_after(table_header, para)
for cell in row.cells:
cell = cell.as_cell()
for paragraph in cell.paragraphs:
paragraph = paragraph.as_paragraph()
# 居中对齐
_para_content = paragraph.to_string(aw.SaveFormat.TEXT)
这些是插入的内容我不想插入第一行数据
已经插入内容了 不想插入第一行数据