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

用 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 中,以供其他人查看和使用。

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

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值