简体   繁体   English

我们如何从任何网站将实时数据导入 excel,然后将该数据导入实时 Python?

[英]How can we import live data into excel from any website and then get that data into live python?

I have some dynamic data in my excel worksheet that keeps updating in 1 to 2 secs, and when I import try to import that data in python with this code:我的 excel 工作表中有一些动态数据会在 1 到 2 秒内不断更新,当我导入时,尝试使用以下代码在 python 中导入该数据:

import pandas as pd
filename = “filename.xlsx”
df = pd.read_excel(filename)
print(df)

It just prints the last manually saved data.I did turn on the auto save function in excel but that automatically saves the data in OneDrive-Personel/documents.它只是打印上次手动保存的数据。我确实在 excel 中打开了自动保存功能,但会自动将数据保存在 OneDrive-Personel/documents 中。 I don't know how to specify the path to that in python.我不知道如何在 python 中指定路径。 I want the dynamic data that is in the excel worksheet right now.我现在想要 excel 工作表中的动态数据。 I appreciate that you people read this, if you did.我很感激你们阅读了这篇文章,如果你这样做了。 Thanks谢谢

Try using VBA to save your file every few seconds instead of autosave.尝试使用 VBA 每隔几秒保存一次文件,而不是自动保存。 It will imitate the manual save, so it should work for you.它会模仿手动保存,所以它应该适合你。

Install openpyxl library and then安装openpyxl库,然后

import pandas as pd
import openpyxl

wb= openpyxl.load_workbook('myfile.xlsx') # opens your file
filename = “filename.xlsx”
df = pd.read_excel(filename)
df.to_excel
print(df)
wb.save("myfile.xlsx") # saves the file
from openpyxl import load_workbook
import xlwings as xw
import time

i = 1
while i == 1:
    wb = load_workbook('filename.xlsx')
    wb.save("filename.xlsx")
    wbxl = xw.Book('filename.xlsx')

    row_num = 2
    while row_num != 13:
        name = wbxl.sheets['sheet1'].range('A{}'.format(row_num)).value
        print(name)
        row_num += 1

    time.sleep(30)

In the code above, when i save it with the openpyxl module the file has to be closed and when i get the data from the closed excel file the data that i recieve is "NA".But if the file is opened then i recieve the required data.在上面的代码中,当我用 openpyxl 模块保存它时,文件必须关闭,当我从关闭的 excel 文件中获取数据时,我收到的数据是“NA”。但是如果文件被打开,那么我收到了所需数据。 Is there any other way to save excel file that works in saving it when it is opened有没有其他方法可以保存excel文件,当它打开时可以保存它

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

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