【发布时间】:2017-11-17 06:33:31
【问题描述】:
我是 Python 的新手,正在从事一个可以使用一些帮助的项目。因此,我正在尝试修改现有的 Excel 工作簿以比较库存数据。幸运的是,有一个在线程序可以检索我需要的所有数据,并且我已经成功地能够提取数据并将数据写入一个新的 Excel 文件。但是,目标是提取数据并将其放入现有的 excel 文件中。此外,我需要覆盖现有文件中的单元格值。我相信 xlwings 能够做到这一点,并且我认为我的代码在正确的轨道上,但是我遇到了意外错误。我得到的错误是:
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
【问题讨论】:
-
pandas.DataFrame.append 有什么帮助?我有我想要的数据框。我无法使用我拥有的数据框覆盖我的 excel 文件中的现有数据。我仍然对如何将数据获取到现有工作簿感到困惑
-
我认为我无法使用这些功能更新现有工作簿。我在网上搜索过,认为使用这些功能是不可能的。很难找到可以修改现有工作场所并替换其中数据的功能。感谢您的帮助和建议。
标签: python excel openpyxl xlwings