简体   繁体   English

使用Python将数据覆盖到现有工作簿

[英]Overwriting data to an existing workbook using Python

I am new to Python and working on a project that I could use some help on. 我是Python的新手,正在从事一个可以在其中使用一些帮助的项目。 So I am trying to modify an existing excel workbook in order to compare stock data. 因此,我试图修改现有的Excel工作簿以比较库存数据。 Luckily, there was a program online that retrieved all the data I need and I have successful been able to pull the data and write the data into a new excel file. 幸运的是,有一个在线程序可以检索我需要的所有数据,而且我已经能够成功提取数据并将数据写入新的excel文件。 However, the goal is to pull the data and put it into an existing excel file. 但是,目标是提取数据并将其放入现有的excel文件中。 Furthermore, I need to overwrite the cell values in the existing file. 此外,我需要覆盖现有文件中的单元格值。 I believe xlwings is able to do this and I think my code is on the right track, but I ran into an unexpected error. 我相信xlwings可以做到这一点,我认为我的代码在正确的轨道上,但是遇到了意外错误。 The error I get is: 我得到的错误是:

TypeError: Objects of type 'Period' can not be converted to a COM VARIANT (but obtaining the buffer() of this object could)

I was wondering if anyone knew why this error came up? 我想知道是否有人知道为什么会出现此错误? Also, does anyone know how to fix it? 另外,有人知道如何解决吗? Is it fixable? 它可以修复吗? Is my code wrong? 我的代码错了吗? Any help or guidance is appreciated. 任何帮助或指导表示赞赏。 Thank you. 谢谢。

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

Data Types : 资料类型

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

Comments : You have a Dict of pandas.DataFrame . 注释 :您有一个pandas.DataFrame 字典

Selecting from a Dict using list(fd_frames.values())[0] does lead to unpredictable Results. 使用list(fd_frames.values())[0]从Dict中进行选择确实会导致不可预测的结果。 Show the Keys of the Dict and choose the one you interested off using these Key, eg: 显示词典的键,然后使用这些键选择您感兴趣的键,例如:

 print(fd_frames.keys())
 >>> dict_keys(['key_1', 'key_2', 'key_n']
 df_2 = fd_frames['key_2']

Beside this, neither of the Dimension in your pandas.DataFrame does match M6:M30 = 25. There are only 8 columns with 20 Values . 除此之外, pandas.DataFrame中的Dimension都不匹配M6:M30 =25。只有8列具有20个Values Therfore you have to align your Worksheet Range to 20 Rows . 因此,您必须将工作表范围对齐为20行 To write Column 2017 to the Worksheet, eg: 将Column 2017写入工作表,例如:

wb['M6:M25'] = df_2['2017'].values

Note : I have updated the code below to accept numpy.ndarray also. 注意 :我已经更新了下面的代码以接受numpy.ndarray


Question : ... the goal is to pull the data and put it into an existing excel file 问题 :...的目标是提取数据并将其放入现有的excel文件中

Update a Workbooks Worksheet Range with List Values. 使用列表值更新工作簿工作表范围。
Using: OpenPyXL : A Python library to read/write Excel 2010 xlsx/xlsm files 使用:OpenPyXL用于读取/写入Excel 2010 xlsx / xlsm文件的Python库

Note : Observe how the List Values have to be arranged! 注意请注意如何排列列表值!
param values: List: *[row 1(col1, ... ,coln), ..., row n(col1, ... ,coln)]` 参数值:列表:* [行1(col1,...,coln),...,行n(col1,...,coln)]`

from openpyxl import Workbook, load_workbook

class UpdateWorkbook(object):
    def __init__(self, fname, worksheet=0):
        self.fname = fname
        self.wb = load_workbook(fname)
        self.ws = self.wb.worksheets[worksheet]

    def save(self):
        self.wb.save(self.fname)

    def __setitem__(self, _range, values):
        """
         Assign Values to a Worksheet Range
        :param _range:  String e.g ['M6:M30']
        :param values: List: [row 1(col1, ... ,coln), ..., row n(col1, ... ,coln)]
        :return: None
        """

        def _gen_value():
            for value in values:
                yield value

            if not isinstance(values, (list, numpy.ndarray)):
                raise ValueError('Values Type Error: Values have to be "list": values={}'.
                                  format(type(values)))
            if isinstance(values, numpy.ndarray) and values.ndim > 1:
                raise ValueError('Values Type Error: Values of Type numpy.ndarray must have ndim=1; values.ndim={}'.
                                  format(values.ndim))

        from openpyxl.utils import range_boundaries
        min_col, min_row, max_col, max_row = range_boundaries(_range)
        cols = ((max_col - min_col)+1)
        rows = ((max_row - min_row)+1)
        if cols * rows != len(values):
            raise ValueError('Number of List Values:{} does not match Range({}):{}'.
                             format(len(values), _range, cols * rows))

        value = _gen_value()
        for row_cells in self.ws.iter_rows(min_col=min_col, min_row=min_row,
                                           max_col=max_col, max_row=max_row):
            for cell in row_cells:
                cell.value = value.__next__()

Usage 用法

 wb = UpdateWorkbook(r'C:\\Users\\vince\\Project\\Spreadsheet.xlsx', worksheet=1) df_2 = fd_frames['key_2'] wb['M6:M25'] = df_2['2017'].values wb.save() 

Tested with Python:3.4.2 - openpyxl:2.4.1 - LibreOffice:4.3.3.2 使用Python:3.4.2-openpyxl:2.4.1-LibreOffice:4.3.3.2测试

Here's how I do a similar procedure for other Stack explorers: 这是我为其他Stack资源管理器执行类似过程的方法:

import pandas as pd
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows

... create your pandas dataframe df...

# Writing from pandas back to an existing EXCEL workbook
# Load workbook
wb = load_workbook(filename=target, read_only=False, keep_vba=True)
ws = wb['Sheet1']

# Overwrite Existing data in sheet with a dataframe.
rows = dataframe_to_rows(df, index=False, header=True)

for r_idx, row in enumerate(rows, 1):
    for c_idx, value in enumerate(row, 1):
         ws.cell(row=r_idx, column=c_idx, value=value)

# Save file
wb.save('outfile.xlsm')

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

相关问题 如何使用python将数据从一个工作表添加到另一个工作簿中的现有工作表而不覆盖其他工作表的数据? - How to add data using python from one sheet to an existing sheet in another workbook without overwriting other sheet's data? 使用 Python OS 模块用新数据覆盖现有的 txt 文件 - Overwriting an existing txt file with new data using Python OS module 将数据保存到新工作表,在现有工作簿中使用 python - save data to new worksheet, in existing workbook using python 使用 python 将多个工作簿中的数据复制并自动化到现有的主工作簿中,而不会丢失格式 - Copy & automate data from multiple workbook into an existing Master workbook without losing formatting using python 使用Python覆盖XLSX文件中的现有单元格 - Overwriting existing cells in an XLSX file using Python 使用Python操作现有的Excel工作簿 - Manipulating Existing Excel Workbook Using Python 使用Python编写和修改现有工作簿 - Writing and modifying an existing workbook using Python .append() 覆盖 python 中存在的 - .append() overwriting existing in python 如何使用python在现有工作簿中复制和粘贴现有工作表? - How to copy and paste existing sheet in existing workbook using python? 如何在python中使用xlsxwriter将数据写入/更新到现有XLSX工作簿的单元格中 - How to write/update data into cells of existing XLSX workbook using xlsxwriter in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM