我是Python新手,正在开发一个项目,我可以使用一些帮助。所以我试图修改一个现有的Excel工作簿以比较库存数据。幸运的是,有一个在线程序可以检索我需要的所有数据,并且我已经成功地将数据提取出来并将数据写入新的excel文件。但是,目标是提取数据并将其放入现有的Excel文件中。此外,我需要覆盖现有文件中的单元格值。我相信xlwings能够做到这一点,我认为我的代码在正确的轨道上,但我遇到了一个意想不到的错误。我得到的错误是:使用Python将数据覆盖到现有工作簿
TypeError: Objects of type 'Period' can not be converted to a COM VARIANT (but obtaining the buffer() of this object could)
我想知道是否有人知道为什么会出现这个错误?另外,有谁知道如何解决它?它可以修复吗?我的代码错了吗?任何帮助或指导表示赞赏。谢谢。
import good_morning as gm
import pandas as pd
import xlwings as xw
#import income statement, balance sheet, and cash flow of AAPL
fd = gm.FinancialsDownloader()
fd_frames = fd.download('AAPL')
#Creates a DataFrame for only the balance sheet
df1 = pd.DataFrame(list(fd_frames.values())[0])
#connects to workbook I want to modify
wb = xw.Book(r'C:\Users\vince\Project\Spreadsheet.xlsm')
#sheet I would like to modify
sht = wb.sheets[1]
#modifies & overwrites values in my spreadsheet(this is where I get the type_error)
sht.range('M6').value = df1
数据类型:
type(fd_frames)
>>> <class 'dict'>
fd_frames.values())[0].info()
>>> <class 'pandas.core.frame.DataFrame'>
RangeIndex: 22 entries, 0 to 21
Data columns (total 8 columns):
parent_index 22 non-null int64
title 22 non-null object
2012 19 non-null float64
2013 20 non-null float64
2014 20 non-null float64
2015 20 non-null float64
2016 20 non-null float64
2017 20 non-null float64
dtypes: float64(6), int64(1), object(1)
memory usage: 1.5+ KB
可能的复制[使用Python编写和修改现有工作簿](https://stackoverflow.com/questions/44489950/writing-and-modifying-an-existing-workbo OK-using-python) –
pandas.DataFrame.append如何提供帮助?我有我想要的数据框。我无法用我拥有的数据框覆盖我的Excel文件中的现有数据。我仍然感到困惑,我如何获取数据到现有的工作簿 – vdub32
我不认为我能够使用这些功能更新现有的工作簿。我在网上搜索过,并不认为有可能通过这些功能来完成。很难找到能够修改现有工作场所并替换其中的数据的功能。感谢您的帮助和建议。 – vdub32