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

问题描述

我的excel工作表中有一些动态数据,这些数据会在1-2秒内保持更新,并且在我导入时尝试使用以下代码在python中导入该数据:

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

它只打印最后手动保存的数据。我确实在excel中打开了自动保存功能,但是会自动将数据保存在OneDrive-Personel /文档中。我不知道如何在python中指定路径。我想要现在在excel工作表中的动态数据。 感谢您阅读本指南,如果您这样做的话。 谢谢

解决方法

尝试使用VBA每隔几秒钟保存一次文件,而不是自动保存。它将模仿手动保存,因此它应该对您有用。

先安装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)

在上面的代码中, 当我使用openpyxl模块保存该文件时,必须关闭该文件,并且当我从已关闭的excel文件中获取数据时,我收到的数据为“ N.A.”。但是,如果文件已打开,那么我会收到所需的数据。还有什么其他方法可以保存excel文件?

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...