简体   繁体   English

如何使用 python 脚本自动将数据集导出到 Power BI 文件

[英]How to Automate export dataset to Power BI file using python script

I have a power BI report that uses SQL server and the data set in xlsx format.我有一个 Power BI 报告,它使用 SQL 服务器和 xlsx 格式的数据集。 I need to export this to dataset when ever I open powerbi file so that it automatically detects the dataset file which is present with the same name and display it using python script.我需要在打开 powerbi 文件时将其导出到数据集,以便它自动检测具有相同名称的数据集文件并使用 python 脚本显示它。

So is there any way possible to automate this procedure.那么有没有可能使这个过程自动化。

Yes, that's possible.是的,这是可能的。 In Power BI Desktop, select File > Options and settings > Options > Python scripting .在 Power BI Desktop 中,选择“文件”>“选项和设置”>“选项”>“Python 脚本” The Python script options page appears, and just choose the python environment you are using.出现 Python 脚本选项页面,只需选择您正在使用的 Python 环境。

To run your Python Script in Power BI Desktop:在 Power BI Desktop 中运行 Python 脚本:

In the Home ribbon, select Get data > Other.在“主页”功能区中,选择“获取数据”>“其他”。

Select Other > Python script as shown in the following image:选择其他 > Python 脚本,如下图所示:

Select Connect .选择连接 Your local latest installed Python version is selected as your Python engine.选择您本地最新安装的 Python 版本作为您的 Python 引擎。 Copy your script into the Python script dialog box that appears.将您的脚本复制到出现的 Python 脚本对话框中。

Here is the official document .这是官方文档

And for reading Excel data.并用于读取 Excel 数据。 I would use openpyxl .我会使用openpyxl Here is a small demo:这是一个小演示:

def xlsx_reader(): 
    data = []
    file_exist = os.path.exists('demo.xlsx')
    if file_exist:
        wb = openpyxl.load_workbook('demo.xlsx')
        wb1 = wb['demo']
        
        data.append(wb1['A1'].value)
    return data

Here is the document for openpyxl.这是 openpyxl 的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM