简体   繁体   English

熊猫:使用to_excel写入现有的Excel文件(xlsx)

[英]pandas: Writing to an existing excel file (xlsx) using to_excel

I have a simple use case for df.to_excel() that I'm struggling with. 我正在苦苦挣扎的df.to_excel()有一个简单的用例。 I want to write to a specific worksheet tab (let's call it "Data") of an existing XLSX workbook, which could be referenced by formulas and pivots on other tabs. 我想写一个现有XLSX工作簿的特定工作表选项卡(我们称其为“数据”),该公式可以由公式和其他选项卡上的枢轴引用。

I've tried to modify ExcelWriter in two ways but both produce errors from openpyxl. 我试图用两种方式修改ExcelWriter,但是两者都会因openpyxl产生错误。

  1. Read an existing sheet using get_sheet_by_name (This errors: "NotImplementedError: use 'iter_rows()' instead".) 使用get_sheet_by_name读取现有工作表(此错误:“ NotImplementedError:改为使用'iter_rows()'”。)
  2. Create a new sheet using create_sheet. 使用create_sheet创建一个新工作表。 (This errors:"ReadOnlyWorkbookException: Cannot create new sheet in a read-only workbook") (此错误:“ ReadOnlyWorkbookException:无法在只读工作簿中创建新工作表”)

     df=DataFrame() from openpyxl.reader.excel import load_workbook book = load_workbook('my_excel_file.xlsx', use_iterators=True) # Assume my_excel_file.xlsx contains a sheet called 'Data' class temp_excel_writer(ExcelWriter): # I need this to inherit the other methods of ExcelWriter in io/parsers.py def __init__(self, path, book): self.book=book test_sheet=self.book.create_sheet(title='Test') # This errors: ReadOnlyWorkbookException self.use_xlsx = True self.sheet_names=self.book.get_sheet_names() self.actual_sheets=self.book.worksheets self.sheets={} for i,j in enumerate(self.sheet_names): self.sheets[j] = (self.actual_sheets[i],1) self.cur_sheet = None self.path = save my_temp_writer=temp_excel_writer('my_excel_file.xlsx', book) df.to_excel(my_temp_writer, sheet_name='Data') 

Any thoughts? 有什么想法吗? Am I missing something obvious? 我是否缺少明显的东西? I'm still in pandas 7.2 我仍然在熊猫7.2

When you load your workbook with use_iterators=True , it then _set_optimized_read() on the Workbook object, which cause it to be loaded read-only. 当您使用use_iterators=True加载工作簿时,它会在Workbook对象上进行_set_optimized_read() ,从而使其以只读方式加载。

Thus, with the following code : 因此,使用以下代码:

from openpyxl.reader.excel import load_workbook

book = load_workbook('t.xlsx', use_iterators=False) # Assume t.xlsx contains ['Data', 'Feuil2', 'Feuil3']
print book.get_sheet_names()


class temp_excel_writer():
    def __init__(self, path, book):
        self.book=book
        test_sheet=self.book.create_sheet(title='Test') # No exception here now
        self.book.save(path)
        self.use_xlsx = True
        self.sheet_names=self.book.get_sheet_names()
        print self.sheet_names
        self.actual_sheets=self.book.worksheets
        self.sheets={}
        for i,j in enumerate(self.sheet_names):
            self.sheets[j] = (self.actual_sheets[i],1)
        self.cur_sheet = None
        self.path = path # I had to modify this line also

my_temp_writer = temp_excel_writer('my_excel_file.xlsx', book)

It create a file named my_excel_file.xlsx and the following output : 它创建一个名为my_excel_file.xlsx的文件和以下输出:

 ['Data', 'Feuil2', 'Feuil3']
 ['Data', 'Feuil2', 'Feuil3', 'Test']

Hope it helps 希望能帮助到你

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

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