用 Python 脚本自动刷新并发布 Power BI Desktop 的程序:

首先,你需要安装 pbixrefreshermsal 模块。可以通过运行以下命令来安装它们:

pip install pbixrefresher
pip install msal

接下来,你需要创建一个 Azure Active Directory 应用程序,并授予该应用程序对 Power BI API 的访问权限。

完成上述步骤后,你可以使用以下脚本来刷新和发布 Power BI Desktop 报表:

import configparser
import os
from msal import ConfidentialClientApplication
from pbixrefresher import Refresher

# 从配置文件中读取应用程序凭证和报表路径
config = configparser.ConfigParser()
config.read('config.ini')
client_id = config['POWERBI']['CLIENT_ID']
client_secret = config['POWERBI']['CLIENT_SECRET']
username = config['POWERBI']['USERNAME']
password = config['POWERBI']['PASSWORD']
report_path = config['POWERBI']['REPORT_PATH']

# 使用应用程序凭证获取访问令牌
authority = 'https://login.microsoftonline.com/common'
app = ConfidentialClientApplication(client_id, authority=authority, client_credential=client_secret)
result = app.acquire_token_for_client(scopes=['https://analysis.windows.net/powerbi/api'])
access_token = result['access_token']

# 刷新报表
refresher = Refresher(report_path, headless=True)
refresher.update_connection_string(access_token)

# 发布报表
os.system(f'pbidesktop.exe "{report_path}" -publish -name "My Published Report" -description "This is my published report" -username "{username}" -password "{password}"')

在上面的脚本中,你需要创建一个 config.ini 配置文件来存储应用程序凭证、报表路径和其他配置信息。该文件应该包含以下内容:

[POWERBI]
CLIENT_ID = <Azure Active Directory 应用程序客户端 ID>
CLIENT_SECRET = <Azure Active Directory 应用程序客户端密码>
USERNAME = <Power BI 用户名>
PASSWORD = <Power BI 密码>
REPORT_PATH = <Power BI Desktop 报表路径>

当你运行脚本时,它将自动刷新指定的报表,并将其发布到 Power BI 中,以供其他人查看和使用。

希望这可以帮助你实现你的目标!

Logo

永洪科技,致力于打造全球领先的数据技术厂商,具备从数据应用方案咨询、BI、AIGC智能分析、数字孪生、数据资产、数据治理、数据实施的端到端大数据价值服务能力。

更多推荐