modules.converters 模块帮助
本章节包含 modules.converters 包中常用「数据结构转换」模块的使用说明和示例,例如:
表格数据转换为地质材料表、剖面数据结构等
不同内部数据结构之间的相互转换
ConvertToMaterialTable
模块简介与适用场景
ConvertToMaterialTable将TableData或TableCollection中的标准地层/岩性表转换为MaterialTable,供地质计算、剖面生成、CAD 出图等模块使用。输入为
TableCollection时,可按table_name选取目标表;未指定时会依次尝试standard_layer_table、标准地层表,再回退到集合中的第一张表。典型场景:GDIM 读入
standard_layer_table后,需转为MaterialTable再接入ConvertToMultiProfile1D、ExportGeoSectionsByBores等下游模块。GProps.LayerNumber列会被强制转为字符串;列名映射与自动匹配规则见后文说明。
端口说明
输入端口 -
InputTable:输入表格(TableData)或表集合(TableCollection)。为None或空集合时,输出为None。输出端口 -
OutputMaterialTable:输出MaterialTable;无法解析输入时为None。
快速上手示例
from gdisdk.dataclass.terminologies import GeoMaterialProps as GProps
from gdisdk.modules.converters import ConvertToMaterialTable
conv = ConvertToMaterialTable(mname="ConvertToMaterialTable")
conv.column_name_map = {
"地层编号": GProps.LayerNumber,
"岩土名称": GProps.MaterialName,
}
conv.sort_by_layer_number = True
conv.reset_material_id = True
# conv.InputTable = ...
conv.execute()
material_table = conv.OutputMaterialTable.data
print(material_table)
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
表格列与 |
|
|
|
是否按 |
|
|
|
排序后是否将 |
|
|
|
输入为 |
列名映射说明
列与 GeoMaterialProps 的对应关系按以下优先级解析:
**显式映射**(
column_name_map):键先按字段名匹配,再按字段标题匹配(与TableData.resolve_columns_map一致)。自动匹配:未在映射中出现的列,先尝试字段名是否为
GeoMaterialProps成员名,再尝试字段标题是否与GeoMaterialProps标题一致。MaterialID:映射或原表中存在
MaterialID列则保留;否则自动生成0..N-1。LayerNumber:匹配成功后强制转为字符串类型。
在 pipeline 中的使用方式
from gdisdk.pipeline.pipeline import PipeLine
from gdisdk.modules.readers import GdimTableReader
from gdisdk.modules.converters import ConvertToMaterialTable
pipeline = PipeLine(app_name="MaterialTableDemo", app_title="标准地层表转MaterialTable")
pipeline.update_gdim_state(token="你的GDIM Token", proj_id="你的GDIM项目ID")
reader = GdimTableReader(mname="ReadTables")
reader.table_fields = ["standard_layer_table"]
conv = ConvertToMaterialTable(mname="ConvertMaterial")
conv.sort_by_layer_number = True
links = reader.OutputTables >> conv.InputTable
pipeline.add_links(links)
pipeline.run()
material_table = conv.OutputMaterialTable.data
print(material_table)
更多信息
下游
MultiProfile1D转换见本章ConvertToMultiProfile1D;剖面导出示例见 modules.geoDataProcess 模块帮助 中的ExportGeoSectionsByBores章节。
ConvertToMultiProfile1D
模块简介与适用场景
ConvertToMultiProfile1D将TableCollection中的 钻孔一览表 与 地层表,结合MaterialTable,组装为MultiProfile1D,供剖切线生成、地形切片、地质剖面出图等下游模块使用。输入表集合须同时包含钻孔表与地层表;表名与列名可按中英文标准字段自动解析,也可通过参数显式指定。
典型场景:GDIM 读入
bore_table/layer_table``(或中文标题 ``钻孔一览表/地层表)及standard_layer_table转成的MaterialTable后,接入CreatePolyLines、ExportGeoSectionsByBores等模块。地层编号会通过
MaterialTable.get_material_id解析为materials_id;若某层编号在材料表中不存在,执行时会抛出异常。
端口说明
输入端口 -
InputTables:输入表集合(TableCollection)。须含钻孔表与地层表;为None时输出为None。 -InputMaterialTable:岩性/标准地层表(MaterialTable),用于将地层编号映射为materials_id;为None时输出为None。输出端口 -
OutputMultiProfile1D:输出MultiProfile1D,内含profiles_table``(钻孔属性)与 ``layers_table``(分层与 ``materials_id);任一必填输入缺失时为None。
快速上手示例
from gdisdk.modules.converters import ConvertToMultiProfile1D
conv = ConvertToMultiProfile1D(mname="ConvertToMultiProfile1D")
# conv.InputTables = table_collection # 含钻孔一览表与地层表
# conv.InputMaterialTable = material_table
# conv.selected_bores = ["ZK1", "ZK2"] # 可选:仅保留指定钻孔
# conv.sort_layers_table = True # 可选:按钻孔号、层底深度排序
conv.execute()
multi_profile = conv.OutputMultiProfile1D.data
print(multi_profile.profiles_table)
print(multi_profile.layers_table)
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
钻孔一览表在集合中的 |
|
|
|
钻孔表五列标识(字段名或字段标题),顺序为:钻孔编号、孔口高程、X、Y、稳定水位埋深。为 |
|
|
|
地层表在集合中的 |
|
|
|
地层表三列标识:钻孔编号、层底深度、地层编号。为 |
|
|
|
若设置,仅保留列表中的钻孔编号(与钻孔表「钻孔编号」列取值一致); |
|
|
|
为 |
表与字段解析说明
钻孔一览表**(``bore_table_field_names`` 未指定时,按顺序匹配字段 **name 或 title):
顺序 |
字段 name |
字段 title |
|---|---|---|
1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
**地层表**(layer_table_field_names 未指定时):
顺序 |
字段 name |
字段 title |
|---|---|---|
1 |
|
|
2 |
|
|
3 |
|
|
组装后的 MultiProfile1D 内部列名:
profiles_table:pnum、top、x_coord、y_coord、gwt_depthlayers_table:pnum、layers_depth、materials_id
在 pipeline 中的使用方式
from gdisdk.pipeline.pipeline import PipeLine
from gdisdk.modules.readers import GdimTableReader
from gdisdk.modules.converters import ConvertToMaterialTable, ConvertToMultiProfile1D
from gdisdk.modules.gisOperators import CreatePolyLines
pipeline = PipeLine(app_name="MultiProfileDemo", app_title="钻孔表转MultiProfile1D")
pipeline.update_gdim_state(token="你的GDIM Token", proj_id="你的GDIM项目ID")
reader = GdimTableReader(mname="ReadTables")
reader.table_fields = ["bore_table", "layer_table", "standard_layer_table"]
convert_material = ConvertToMaterialTable(mname="ConvertMaterial")
convert_material.sort_by_layer_number = True
convert_bores = ConvertToMultiProfile1D(mname="ConvertBores")
create_lines = CreatePolyLines(mname="CreatePolyLines")
links = (
reader.OutputTables >> convert_material.InputTable
| reader.OutputTables >> convert_bores.InputTables
| convert_material.OutputMaterialTable >> convert_bores.InputMaterialTable
| convert_bores.OutputMultiProfile1D >> create_lines.InputMultiProfile1D
)
pipeline.add_links(links)
pipeline.run()
multi_profile = convert_bores.OutputMultiProfile1D.data
print(multi_profile)
更多信息
上游材料表转换见本章
ConvertToMaterialTable;剖切线与剖面出图见 modules.geoDataProcess 模块帮助 中的SliceTerrain、ExportGeoSectionsByBores章节。
PdfToImages
模块简介与适用场景
PdfToImages用于将一个或多个 PDF 文件按页转换为图片文件(PNG/JPG/JPEG)。适合在 pipeline 中将 PDF 扫描件/报表等转为图片,供后续识别、标注、可视化或归档。
端口说明
输入端口 - (无):该模块主要通过初始化参数
pdf_configs/output_dir/dpi/format提供输入配置输出端口 -
OutputImages:输出图片文件路径列表(list[str],图片格式由format决定)
快速上手示例
from gdisdk.modules.converters import PdfToImages
pdf2img = PdfToImages(mname="PdfToImages")
pdf2img.pdf_configs = [
{
"file_path": "doc1.pdf",
"image_prefix": "doc1",
"first_page": 1,
"last_page": 3,
},
{"file_path": "doc2.pdf"}, # 仅指定文件路径,其余使用默认值
]
pdf2img.dpi = 200
pdf2img.format = "png"
pdf2img.output_dir = "output/pdf_images"
pdf2img.execute()
print(pdf2img.OutputImages.data)
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
PDF 转换配置列表。每一项至少包含
|
|
|
|
输出目录。若 pipeline 设置了 |
|
|
|
转换 DPI(越大图片越清晰但更大)。 |
|
|
|
输出图片格式。 |
在 pipeline 中的使用方式
from gdisdk.pipeline.pipeline import PipeLine
from gdisdk.modules.converters import PdfToImages
pipeline = PipeLine(app_name="PdfToImagesDemo", app_title="PDF转图片示例")
pipeline.workspace = "workspace" # 优先于 PdfToImages.output_dir
pdf2img = PdfToImages(mname="PdfToImages")
pdf2img.pdf_configs = [{"file_path": "doc1.pdf", "first_page": 1, "last_page": 2}]
pdf2img.dpi = 200
pdf2img.format = "png"
pipeline.add_module(pdf2img)
pipeline.run()
print(pdf2img.OutputImages.data)
更多信息
TableToMarkdown
模块简介与适用场景
TableToMarkdown用于将TableData或TableSeries转换为 GitHub Flavored Markdown 表格文本。适合将结构化表格数据直接提供给 LLM、前端 Markdown 渲染器、报告拼装模块或文本文件写出模块。
可选将结果同时封装为
ResultModel输出到OutputResultModel。
端口说明
输入端口 -
InputTable:输入表格(TableData或TableSeries)输出端口 -
OutputMarkdown:输出 Markdown 文本(str) -OutputResultModel:输出结果模型(ResultModel,包含 1 个字符串字段,其值为 Markdown 文本)
快速上手示例
from gdisdk.modules.converters import TableToMarkdown
to_md = TableToMarkdown(mname="TableToMarkdown")
to_md.columns = ["孔号", "深度", "地层"]
to_md.use_titles = True
to_md.include_units = True
to_md.include_table_title = True
to_md.show_index = False
# to_md.InputTable = ...
to_md.execute()
print(to_md.OutputMarkdown.data)
print(to_md.OutputResultModel.data)
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
需要输出的列,可使用字段名或字段标题;传入列表时也会按该顺序重排列。仅当输入为 |
|
|
|
是否使用字段标题作为 Markdown 表头;为 |
|
|
|
是否在表头后追加单位,例如 |
|
|
|
是否在表格前输出表标题。默认使用 Markdown 二级标题 |
|
|
|
是否将表标题输出为居中的 HTML 标题 |
|
|
|
是否把 DataFrame 行索引作为第一列输出。对 LLM 输入场景通常建议保持 |
|
|
|
数值列写入 Markdown 时的小数位数。 |
|
|
|
是否在含 LaTeX 命令或上下标的字符串外包裹 |
|
|
|
|
|
|
|
|
在 pipeline 中的使用方式
from gdisdk.pipeline.pipeline import PipeLine
from gdisdk.modules.filters import TableSelector
from gdisdk.modules.readers import GdimTableReader
from gdisdk.modules.converters import TableToMarkdown
pipeline = PipeLine(app_name="TableToMarkdownDemo", app_title="表格转Markdown")
read_tables = GdimTableReader(mname="ReadTables")
read_tables.table_fields = ["钻孔一览表"]
pick = TableSelector(mname="PickTable")
pick.table_name = "钻孔一览表"
to_md = TableToMarkdown(mname="ToMarkdown")
to_md.include_units = True
to_md.show_index = False
pipeline.add_links(
read_tables.OutputTables >> pick.InputTables
| pick.OutputTable >> to_md.InputTable
)
pipeline.run()
print(to_md.OutputMarkdown.data)
更多信息
TablesToMarkdown
模块简介与适用场景
TablesToMarkdown用于将TableCollection转换为一个或多个 Markdown 表格文本块。当集合存在主子表关系时,可自动合并展示(更适合钻孔主表 + 分层子表等场景);
merge_mode可在 LLM 友好的 Markdown 与带rowspan的 HTML 表格之间切换。列筛选、精度、LaTeX 等行为与
TableToMarkdown一致。
端口说明
输入端口 -
InputTables:输入表格集合(TableCollection)输出端口 -
OutputMarkdown:输出完整 Markdown 文本(str,多个表块使用separator拼接) -OutputResultModel:输出结果模型(ResultModel,包含 1 个字符串字段,其值为完整 Markdown 文本)
快速上手示例
from gdisdk.modules.converters import TablesToMarkdown
to_md = TablesToMarkdown(mname="TablesToMarkdown")
to_md.combine_related = True
to_md.merge_mode = "markdown"
to_md.use_titles = True
to_md.include_units = True
to_md.separator = "\n\n---\n\n"
# to_md.InputTables = ...
to_md.execute()
print(to_md.OutputMarkdown.data)
print(to_md.OutputResultModel.data)
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
输入表格集合;也可通过端口 |
|
|
|
当表格集合中存在主子表关系且配置了主键时,是否按关系合并展示;为 |
|
|
|
仅对合并后的主子表块生效。 |
|
|
|
按表配置列筛选。key 可为表的 |
|
|
|
合并主子表时的展示模式。 |
|
|
|
是否使用字段标题作为表头;为 |
|
|
|
是否在表头后追加字段单位。 |
|
|
|
是否在每个表块前输出标题。 |
|
|
|
是否将标题渲染为居中的 HTML 标题,而不是普通 |
|
|
|
是否输出行索引。仅在“独立渲染每张表”时生效,对合并块无效。 |
|
|
|
多个表块之间的分隔文本。 |
|
|
|
数值列小数位数。可与 |
|
|
|
与 |
|
|
|
|
|
|
|
|
在 pipeline 中的使用方式
from gdisdk.pipeline.pipeline import PipeLine
from gdisdk.modules.readers import GdimTableReader
from gdisdk.modules.converters import TablesToMarkdown
pipeline = PipeLine(app_name="TablesToMarkdownDemo", app_title="表格集合转Markdown")
read_tables = GdimTableReader(mname="ReadTables")
read_tables.table_fields = ["钻孔一览表", "地层信息表"]
to_md = TablesToMarkdown(mname="ToMarkdown")
to_md.combine_related = True
to_md.merge_mode = "html"
pipeline.add_links(read_tables.OutputTables >> to_md.InputTables)
pipeline.run()
print(to_md.OutputMarkdown.data)
更多信息
DocxToMarkdown
模块简介与适用场景
DocxToMarkdown用于将 Word 文档(.docx)或 RTF 文档(.rtf)转换为 Markdown 文本。适合 LLM / 多模态模型后续处理;输入既可以是本地文件路径,也可以是 GDIM MinIO 文件信息字典(模块会自动下载后再转换)。
提取图片时可将 Markdown 中的图片链接改写为相对路径,便于连同
.md与图片目录一并拷贝或上传(详见images_dir等参数)。
端口说明
输入端口 -
InputDocxFile:输入文档路径(.docx/.rtf)输出端口 -
OutputMarkdown:输出 Markdown 文本内容 -OutputResultModel:结构化结果(ResultModel,仅包含 1 个字符串字段,字段值为 Markdown 文本)
快速上手示例
from gdisdk.modules.converters import DocxToMarkdown
conv = DocxToMarkdown(mname="DocxToMarkdown")
conv.input_file = "report.docx"
conv.result_key_name = "report_markdown"
conv.result_key_title = "报告 Markdown"
conv.markdown_format = "gfm"
conv.extract_images = True
conv.images_dir = "images"
conv.wrap_text = False
conv.execute()
print(conv.OutputMarkdown.data)
result = conv.OutputResultModel.data
print(result.report_markdown)
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
输入文档路径;也可通过端口 |
|
|
|
|
|
|
|
|
|
|
|
Markdown 格式。 |
|
|
|
是否提取图片到目录并在 Markdown 中引用(默认会做相对路径改写,便于打包移动;见 |
|
|
|
图片输出**目录**。为 |
|
|
|
当 |
|
|
|
仅当 |
|
|
|
是否将图片以 base64 data URI 形式嵌入 Markdown(会覆盖 |
|
|
|
是否对输出文本自动换行。默认会附加 |
|
|
|
额外传给 Pandoc 的命令行参数(高级用法)。例如 |
图片与 Markdown 保存位置(简要约定)
images_dir_relative_to_input=True、且images_dir为默认或相对路径:建议将导出的.md与输入文档放在**同一目录**(或与所选根目录一致),使 Markdown 图片语法与 HTML<img>中src的相对路径能解析到images``(或 ``{stem}_images)下的文件。images_dir为绝对路径且relative_image_link=True:将.md保存在 **Path(images_dir).parent``**(即图片文件夹的上一级),与 ``relative_image_link的语义一致。
在 pipeline 中的使用方式
from gdisdk.pipeline.pipeline import PipeLine
from gdisdk.modules.converters import DocxToMarkdown
pipeline = PipeLine(app_name="DocxToMarkdownDemo", app_title="Docx转Markdown")
conv = DocxToMarkdown(mname="DocxToMarkdown")
conv.input_file = "report.docx"
conv.result_key_name = "report_markdown"
conv.extract_images = True
conv.markdown_format = "gfm"
pipeline.add_module(conv)
pipeline.run()
print(conv.OutputMarkdown.data)
print(conv.OutputResultModel.data)
Note
当一个 pipeline 中有多个 DocxToMarkdown,且后续模块需要按字段名读取 Markdown 内容时,
建议为每个模块设置不同的 result_key_name,避免动态字段名冲突。
上传 GDIM 前检查
是否需要特别处理:当该模块在 GDIM 平台运行,且输入文档来自前端上传文件时,是。
必须检查:
不要依赖仅本地存在的固定路径;上传到 GDIM 后,推荐由前端传入 GDIM 文件描述
dict,并直接赋给input_file;文件建议通过 pipeline attribute + 前端传入的 GDIM 文件描述(``dict``) 提供,这是上传后的推荐方式。
建议检查:
如需提取图片:优先使用默认
images_dir``(``None)并保持images_dir_relative_to_input=True,使图片目录与下载后的输入文件在同一父目录下;extra_args中避免依赖仅在本地机器有效的路径或系统资源,尽量只使用与 Pandoc 转换本身相关的参数;
若遗漏,常见现象:
本地调试时能读取
D:\\...\\report.docx,上传 GDIM 后因服务器看不到该路径而失败;指定了不可写或不存在的
images_dir,导致图片提取失败或 Markdown 中图片引用异常;
更多信息
TableToDataFrame
模块简介与适用场景
TableToDataFrame将TableData``(带字段元信息:title/unit/description 等)转换为纯 ``pandas.DataFrame。适合把 pipeline 的表格数据接到只接受 DataFrame 的第三方库/自定义脚本中。
端口说明
输入端口 -
InputTable:输入表格(TableData)输出端口 -
OutputGeneralTable:输出 ``pandas.DataFrame``(纯 DataFrame,不含字段元信息)
快速上手示例
from gdisdk.modules.converters import TableToDataFrame
conv = TableToDataFrame(mname="TableToDataFrame")
conv.use_titles_as_columns = True
conv.preserve_index = True
# conv.InputTable = ...
conv.execute()
df = conv.OutputGeneralTable.data
print(df)
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
是否使用字段标题(title)作为 DataFrame 列名;否则使用字段名(name)。 |
|
|
|
是否保留原始索引;为 |
在 pipeline 中的使用方式
from gdisdk.pipeline.pipeline import PipeLine
from gdisdk.modules.readers import GdimTableReader
from gdisdk.modules.filters import TableSelector
from gdisdk.modules.converters import TableToDataFrame
pipeline = PipeLine(app_name="ToDFDemo", app_title="TableData转DataFrame")
read_tables = GdimTableReader(mname="ReadTables")
read_tables.table_fields = ["某表"]
pick = TableSelector(mname="PickTable")
pick.table_name = "某表"
to_df = TableToDataFrame(mname="ToDF")
to_df.use_titles_as_columns = True
links = (
read_tables.OutputTables >> pick.InputTables
| pick.OutputTable >> to_df.InputTable
)
pipeline.add_links(links)
pipeline.run()
df = to_df.OutputGeneralTable.data
print(df)
更多信息
TableToJson
模块简介与适用场景
TableToJson用于将TableData/TableSeries通过其serialize()方法转换为 JSON 对象(dict)。同时会输出一份 JSON 字符串 到
OutputResultModel,便于直接喂给 LLM 做表格内容分析。
端口说明
输入端口 -
InputTable:输入表格(TableData)或单列序列(TableSeries)输出端口 -
OutputJsonObject:JSON 对象(dict) -OutputResultModel:结构化结果(ResultModel,仅包含 1 个字段,其值为 JSON 字符串)
快速上手示例
from gdisdk.modules.converters import TableToJson
to_json = TableToJson(mname="TableToJson")
to_json.columns = ["字段A", "字段B"] # 可用字段 name 或 title;仅对 TableData 生效
to_json.indent = 2
to_json.result_key_name = "table_json"
to_json.result_key_title = "表格JSON"
# to_json.InputTable = ...
to_json.execute()
print(to_json.OutputJsonObject.data) # dict
print(to_json.OutputResultModel.data) # ResultModel(字段值是 JSON 字符串)
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
需要转换的列名(可为字段 name 或 title)。为空表示全部列。仅当输入为 TableData 时生效;输入为 TableSeries 时忽略该参数。 |
|
|
|
|
|
|
|
|
|
|
|
JSON 文本缩进。为空时输出紧凑格式;设为 2/4 可读性更好(文本更长)。 |
在 pipeline 中的使用方式
from gdisdk.pipeline.pipeline import PipeLine
from gdisdk.modules.readers import GdimTableReader
from gdisdk.modules.filters import TableSelector
from gdisdk.modules.converters import TableToJson
pipeline = PipeLine(app_name="TableToJsonDemo", app_title="表格转JSON示例")
read_tables = GdimTableReader(mname="ReadTables")
read_tables.table_fields = ["某表"]
pick = TableSelector(mname="PickTable")
pick.table_name = "某表"
to_json = TableToJson(mname="ToJson")
to_json.columns = None
to_json.indent = 2
links = (
read_tables.OutputTables >> pick.InputTables
| pick.OutputTable >> to_json.InputTable
)
pipeline.add_links(links)
pipeline.run()
print(to_json.OutputJsonObject.data)
print(to_json.OutputResultModel.data)
更多信息
JsonToTable
模块简介与适用场景
JsonToTable用于将 JSON 对象(dict)或 JSON 字符串(str)转换为TableData或TableSeries。会自动判断 JSON 格式并优先走
TableSeries.deserialize/TableData.deserialize;若不是内部序列化格式,会回退尝试pandas.DataFrame(json_object)再封装为TableData。
端口说明
输入端口 -
InputJsonObject:输入 JSON(dict或 JSON 字符串)输出端口 -
OutputTable:输出表格(TableData或TableSeries)
快速上手示例
from gdisdk.modules.converters import JsonToTable
to_table = JsonToTable(mname="JsonToTable")
to_table.single_column_as = "auto"
# 既支持 dict,也支持 JSON 字符串
to_table.InputJsonObject = '{"a":[1,2], "b":[3,4]}'
to_table.execute()
print(type(to_table.OutputTable.data), to_table.OutputTable.data)
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
输入 JSON;也可通过端口 |
|
|
|
单列数据输出策略:
|
在 pipeline 中的使用方式
from gdisdk.pipeline.pipeline import PipeLine
from gdisdk.modules.converters import TableToJson, JsonToTable
pipeline = PipeLine(app_name="JsonRoundTripDemo", app_title="JSON与表格互转示例")
to_json = TableToJson(mname="ToJson")
to_json.indent = None
to_table = JsonToTable(mname="ToTable")
to_table.single_column_as = "auto"
pipeline.add_links(
to_json.OutputJsonObject >> to_table.InputJsonObject
)
pipeline.run()
print(to_table.OutputTable.data)
更多信息
TableToSingleResult
模块简介与适用场景
TableToSingleResult用于从TableData中提取指定的**一行**或**一列**,并转换为SingleResult。适合把表格中的关键指标(某列/某行)抽成结果对象,供报告、文本模板、结果汇总等环节使用。
端口说明
输入端口 -
InputTable:输入表格(TableData)输出端口 -
OutputSingleResult:输出结果(SingleResult,包含 1 个或多个UnitResult,取决于result_mode)
快速上手示例
from gdisdk.modules.converters import TableToSingleResult
# 从第 0 行提取:为每个列生成一个 UnitResult(默认 multiple)
conv = TableToSingleResult(mname="TableToSingleResult")
conv.axis = "row"
conv.row_index = 0
conv.result_mode = "multiple"
# conv.InputTable = ...
conv.execute()
print(conv.OutputSingleResult.data)
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
提取方向:按行或按列。 |
|
|
|
当 |
|
|
|
当 |
|
|
|
|
|
|
|
仅在 |
|
|
|
仅在 |
|
|
|
仅当 |
在 pipeline 中的使用方式
from gdisdk.pipeline.pipeline import PipeLine
from gdisdk.modules.converters import TableToSingleResult
pipeline = PipeLine(app_name="ToSingleResultDemo", app_title="表格转SingleResult")
conv = TableToSingleResult(mname="ToSingleResult")
conv.axis = "column"
conv.column_index = "某指标"
conv.result_mode = "single"
# links = upstream.OutputTable >> conv.InputTable
# pipeline.add_links(links)
# pipeline.run()
更多信息
TableToResultModel
模块简介与适用场景
TableToResultModel用于把TableData的某一行转换为动态生成的ResultModel。输入表中的每一列都会变成结果模型中的一个字段,并自动继承原字段的
title、description和unit元数据,同时根据 pandas dtype 推断 Python 类型。适合把“单行表格”转换成结构化结果对象,供下游模块、模板渲染或接口返回直接使用。
端口说明
输入端口 -
InputTable:输入表格(TableData)输出端口 -
OutputResultModel:输出结果模型(ResultModel)
快速上手示例
from gdisdk.modules.converters import TableToResultModel
conv = TableToResultModel(mname="TableToResultModel")
conv.row_index = 0
conv.model_name = "ProjectSummaryModel"
# conv.InputTable = ...
conv.execute()
print(conv.OutputResultModel.data)
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
要转换的行。传 |
|
|
|
动态生成的 |
空表行为说明
当输入为
None时,模块输出None。当表格没有任何列时,模块返回一个不含字段的空
ResultModel。当表格有列但没有行时,模块仍会按列结构创建
ResultModel,只是所有字段值都为None;这对“先保留 schema、后续再补值”的场景很有用。
在 pipeline 中的使用方式
from gdisdk.pipeline.pipeline import PipeLine
from gdisdk.modules.readers import GdimTableReader
from gdisdk.modules.filters import TableSelector
from gdisdk.modules.converters import TableToResultModel
pipeline = PipeLine(app_name="TableToResultModelDemo", app_title="表格转ResultModel")
read_tables = GdimTableReader(mname="ReadTables")
read_tables.table_fields = ["项目基本信息表"]
pick = TableSelector(mname="PickTable")
pick.table_name = "项目基本信息表"
to_result_model = TableToResultModel(mname="ToResultModel")
to_result_model.row_index = 0
to_result_model.model_name = "ProjectInfoModel"
links = (
read_tables.OutputTables >> pick.InputTables
| pick.OutputTable >> to_result_model.InputTable
)
pipeline.add_links(links)
pipeline.run()
result_model = to_result_model.OutputResultModel.data
print(result_model)
更多信息
SingleResultToText
模块简介与适用场景
SingleResultToText将SingleResult按模板格式化为文本。适合生成报告段落、结论描述、日志输出等。
端口说明
输入端口 -
InputSingleResult:输入结果对象(SingleResult)输出端口 -
OutputText:输出格式化文本(str)
快速上手示例
from gdisdk.modules.converters import SingleResultToText
to_text = SingleResultToText(mname="SingleResultToText")
to_text.text_template = "计算结果:{result_a},精度={result_b:.3f}"
to_text.joiner_for_list_value = ", "
# to_text.InputSingleResult = ...
to_text.execute()
print(to_text.OutputText.data)
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
文本模板。占位符来自 SingleResult 中 UnitResult 的 name / title;支持格式化(如 |
|
|
|
当某个 UnitResult 的 value 为 list 时,使用该连接符拼接成字符串再参与模板格式化。 |
在 pipeline 中的使用方式
from gdisdk.pipeline.pipeline import PipeLine
from gdisdk.modules.converters import SingleResultToText
pipeline = PipeLine(app_name="SingleResultToTextDemo", app_title="结果转文本")
to_text = SingleResultToText(mname="ToText")
to_text.text_template = "结论:{结论}"
# links = upstream.OutputSingleResult >> to_text.InputSingleResult
# pipeline.add_links(links)
# pipeline.run()
更多信息
SingleResultToTable
模块简介与适用场景
SingleResultToTable将SingleResult转为只有一行的TableData:每个 UnitResult 对应一列。适合把结果对象回写为表格,以便保存、展示或进一步表格化处理。
端口说明
输入端口 -
InputSingleResult:输入结果对象(SingleResult)输出端口 -
OutputTable:输出单行表格(TableData,每个UnitResult对应一列)
快速上手示例
from gdisdk.modules.converters import SingleResultToTable
conv = SingleResultToTable(mname="SingleResultToTable")
conv.table_name = "single_result"
conv.table_title = "结果表"
conv.joiner = ","
# conv.InputSingleResult = ...
conv.execute()
print(conv.OutputTable.data)
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
输出表 name;为空时使用默认 |
|
|
|
输出表 title;为空时使用默认 |
|
|
|
当某个 UnitResult 的 value 为 list 时,使用该连接符拼接为单个字符串写入表格单元。 |
在 pipeline 中的使用方式
from gdisdk.pipeline.pipeline import PipeLine
from gdisdk.modules.converters import SingleResultToTable
pipeline = PipeLine(app_name="SingleResultToTableDemo", app_title="结果转表格")
conv = SingleResultToTable(mname="ToTable")
conv.table_name = "R"
conv.table_title = "结果表"
# links = upstream.OutputSingleResult >> conv.InputSingleResult
# pipeline.add_links(links)
# pipeline.run()
更多信息
ResultModelToTable
模块简介与适用场景
ResultModelToTable将ResultModel``(或其子类)转为只有一行的 ``TableData:每个模型字段对应一列,字段的title与unit会写入FieldMetadata。与
SingleResultToTable类似,都用于把结构化结果回写为表格;区别在于本模块面向ResultModel类型化字段,而非SingleResult中的UnitResult列表。典型场景:上游模块输出
ResultModel``(如 ``GdimAppProjectInfoReader的项目信息),需转为TableData再写入 GDIM/MDB 或并入TableCollection。
端口说明
输入端口 -
InputResultModel:输入结构化结果(ResultModel或其子类)。为None时,输出为None。输出端口 -
OutputTable:输出单行表格(TableData,每个模型字段对应一列)
快速上手示例
from gdisdk.modules.converters import ResultModelToTable
conv = ResultModelToTable(mname="ResultModelToTable")
conv.table_name = "app_project_information"
conv.table_title = "项目信息表"
conv.joiner = ","
# conv.InputResultModel = ...
conv.execute()
print(conv.OutputTable.data)
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
输出表 |
|
|
|
输出表 |
|
|
|
当某个字段值为 list 时,使用该连接符拼接为单个字符串写入表格单元。 |
在 pipeline 中的使用方式
from gdisdk.pipeline.pipeline import PipeLine
from gdisdk.modules.converters import ResultModelToTable
pipeline = PipeLine(app_name="ResultModelToTableDemo", app_title="ResultModel 转表格")
conv = ResultModelToTable(mname="ToTable")
conv.table_name = "app_project_information"
conv.table_title = "项目信息表"
# links = upstream.OutputResultModel >> conv.InputResultModel
# pipeline.add_links(links)
# pipeline.run()
更多信息
TableToString
模块简介与适用场景
TableToString用字符串模板把TableData按行描述成文本,并将一个或多个模板的结果汇总为ResultModel输出。
Note
需要显式设置
templates;未设置时模块会直接输出None。
端口说明
输入端口 -
InputTable:输入表格(TableData)输出端口 -
OutputResultModel:输出结果(ResultModel,每个模板 key 生成一个字段,其值为拼接后的描述文本)
快速上手示例
from gdisdk.modules.converters import TableToString
conv = TableToString(mname="TableToString")
conv.templates = {
"simple": "{item} has {count}",
"detail": "Item {item} count={count:.2f}",
}
conv.result_titles = {"simple": "简述", "detail": "详细描述"}
conv.separator = "\n"
conv.prefix = "Summary:\n"
conv.suffix = "\n--END--"
conv.include_empty_rows = False
# conv.InputTable = ...
conv.execute()
print(conv.OutputResultModel.data)
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
模板字典:key 作为 |
|
|
|
模板结果标题映射:key 为模板 key,value 为输出字段的 title;为空则用 key 作为 title。 |
|
|
``, `` |
多行描述的连接符(如 |
|
|
|
可选前后缀,添加到最终整段描述的开头/结尾。 |
|
|
|
是否保留 “模板所有字段都为空值” 的行;为 行为说明:
|
模板语法说明
模板使用 Python 的 .format() 方法进行格式化,支持以下语法:
基本变量替换:使用
{变量名}或{字段标题}作为占位符templates = { "desc": "{geological_age}({geological_age_symbol})" } # 输出示例:第四系(Q)
格式化数字:使用 Python 格式化语法
templates = { "desc": "数值:{value:.2f}" } # 输出示例:数值:3.14
输出字面量花括号:使用双花括号
{{和}}templates = { "desc": "格式:{{literal_text}}" } # 输出:格式:{literal_text}
变量值包裹在花括号中:使用三花括号
{{{变量名}}}templates = { "layer_overview": "{geological_age}({geological_age_symbol}^{{{geological_origin_symbol}}})" } # 假设 geological_origin_symbol = "apl" # 输出示例:第四系(Q^{apl})
Note
{{{variable}}}会先解析{{为字面量{,然后{variable}被替换为变量值,最后}}被解析为字面量}这样可以将变量的值包裹在花括号中输出
跳过包含空值的行:使用
include_empty_rows=False# 表格数据(部分行有空值) data = pd.DataFrame({ "geological_age": ["第四系", "第三系", "第二系", "侏罗系", None], "geological_age_symbol": ["Q", "E", "N", "J", None], "geological_origin_symbol": ["apl", None, "", "m", None], }) table = TableData(data) converter = TableToString(mname="TableToString") converter.templates = { "layer_overview": "{geological_age}({geological_age_symbol}^{{{geological_origin_symbol}}})" } converter.separator = "、" converter.include_empty_rows = False # 跳过所有字段都为空的行 converter.InputTable = table converter.execute() result = converter.OutputResultModel.data # 输出:第四系(Q^{apl})、第三系(E^{})、第二系(N^{})、侏罗系(J^{m}) # 注意: # - 第1-4行保留(至少有一个非空字段),空值显示为空字符串 # - 第5行被跳过(所有字段都为 None)
在 pipeline 中的使用方式
from gdisdk.pipeline.pipeline import PipeLine
from gdisdk.modules.converters import TableToString
pipeline = PipeLine(app_name="TableToStringDemo", app_title="表格转描述文本")
conv = TableToString(mname="ToString")
conv.templates = {"desc": "{字段A}={字段B}"}
conv.separator = "; "
# links = upstream.OutputTable >> conv.InputTable
# pipeline.add_links(links)
# pipeline.run()
更多信息