简体   繁体   中英

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:

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. I don't know how to specify the path to that in python. I want the dynamic data that is in the excel worksheet right now. I appreciate that you people read this, if you did. Thanks

Try using VBA to save your file every few seconds instead of autosave. It will imitate the manual save, so it should work for you.

Install openpyxl library and then

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. Is there any other way to save excel file that works in saving it when it is opened

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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