A:由于Exportfile2是一个本地交互的功能,其实现依赖天软本地函数包,而pyTSL是不依赖天软客户端相关模块的,与天软交互过程是纯与天软服务器交互的实现。
因此,在使用pyTSL模块交互时,执行串中不能包括rdo2等操作,除非有在python提前实现并注册。
如若需要在交互中实现如Exportfile2等本地交互(rdo2)的功能,则需要在交互前在python中先实现该类模型的功能,并注册为天软函数才可调用。
目前在pyTSL模块中已实现注册importfile与exportfile函数,若用户不需要指定Sheet功能,则可考虑改用exportfile实现导出。
这里以实现一个Exportfile2的功能为例:
在python中定义一个pyExportfile2模型,实现将数据导出到指定Excel中的指定Sheet中。
(注:其它Exportfile2更多的功能,如指定位置等这里实现不考虑,如有这类需求,建议取数后通过python相关模块实现)。
实现步骤为:
第一步:python中定义pyExportfile2(data,file_path,sheet_name),并实现导出功能。
第二步:通过register_proc接口注册为天软函数
第三步:在call等接口的交互串中调用pyExportfile2导出数据。
实现与测试代码如下:
import pandas as pd
import datetime as dt
import os
import pyTSL as ts
import time
c=ts.Client("xxx","xxx","tsl.tinysoft.com.cn",443)
rt=c.login()
print("login tsl")
print(c.last_error())
#定义函数pyExportfile2,实现导出数据到指定sheet中,天软中类Exportfile2功能
def pyExportfile2(data,file_path,sheet_name):
df = pd.DataFrame(data) # 将数组转换为DataFrame
try:
# 检查文件是否存在
if os.path.exists(file_path):
# 文件存在,以追加模式打开
with pd.ExcelWriter(file_path, engine='openpyxl', mode='a', if_sheet_exists='replace') as writer:
df.to_excel(writer, sheet_name=sheet_name, index=False)
else:
# 文件不存在,创建新文件
with pd.ExcelWriter(file_path, engine='openpyxl') as writer:
df.to_excel(writer, sheet_name=sheet_name, index=False)
print(f"✓ 成功导出到 {file_path} 的 '{sheet_name}' sheet")
return True
except Exception as e:
print(f"✗ 导出失败: {e}")
return False
#注册为天软函数
ts.register_proc("pyExportfile2", pyExportfile2)
tsl='''
function getdata();
begin
// LJ:="E:\\\\test\\\\pyData.xlsx";
LJ:="E:/test/pyData数据.xlsx"; //注:这里必须是四个\或一个/
a:=rand(3,array("A","B"));
b:=ones(3,array("A","B"));
c:=zeros(3,array("A","B"));
r1:=rdo2 pyExportfile2(a,LJ,"表A");
r2:=rdo2 pyExportfile2(b,LJ,"表B");
r3:=rdo2 pyExportfile2(c,LJ,"表C");
return array(r1,r2,r3);
end;
'''
r=c.call("getdata",code=tsl)
if r.error():
print(r.message())
else:
print(r.value())
测试结果:
