你好,我有这样的代码,我想保存为同名,但添加一个Tag New到相同的文件名,所以保存为new_TBM。任何帮助表示赞赏。
import sys
from tableaudocumentapi import Workbook
sourceWB = Workbook('C:\\Users\\rmakkena\\Music\\TBM.twb')
sourceWB.datasources[0].connections[0].server = "MY-NEW-SERVER"
sourceWB.datasources[0].connections[0].dbname = "NEW-DATABASE"
sourceWB.datasources[0].connections[0].username = "rithesh"
sourceWB.save_as()
答案 0 :(得分:1)
您只需要对( basename 部分)文件名进行一些处理。为此,我们使用[Python]: os.path — Common pathname manipulations。怎么做:
(稍加修改)代码(选中[GitHub]: document-api-python/tableaudocumentapi/workbook.py:def save_as(self, new_filename):
):
import sys
import os
from tableaudocumentapi import Workbook
file_name = "C:\\Users\\rmakkena\\Music\\TBM.twb"
new_tag = "new_"
sourceWB = Workbook(file_name)
sourceWB.datasources[0].connections[0].server = "MY-NEW-SERVER"
sourceWB.datasources[0].connections[0].dbname = "NEW-DATABASE"
sourceWB.datasources[0].connections[0].username = "rithesh"
sourceWB.save_as(os.path.join(os.path.dirname(file_name), new_tag + os.path.basename(file_name)))