Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8416993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:55:17+00:00 2026-06-10T01:55:17+00:00

I have a simple use case for df.to_excel() that I’m struggling with. I want

  • 0

I have a simple use case for df.to_excel() that I’m struggling with. 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.

I’ve tried to modify ExcelWriter in two ways but both produce errors from openpyxl.

  1. Read an existing sheet using get_sheet_by_name (This errors: “NotImplementedError: use ‘iter_rows()’ instead”.)
  2. Create a new sheet using create_sheet. (This errors:”ReadOnlyWorkbookException: Cannot create new sheet in a read-only workbook”)

    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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:55:19+00:00Added an answer on June 10, 2026 at 1:55 am

    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.

    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 :

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

    Hope it helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple use case where I want to grab a session variable
I have a simple use case with p:selectOneButton . I basically want one option
I have a simple use case to solve. Imagine that somebody tells you hey,
We have a simple requirement to use https for certain specific pages in a
In Java I use getters/setters when I have simple models/pojos. I find that the
we have a simple REST request that I can use manually on my Mac
I have a simple function that returns an NSString after decoding it. I use
I have a simple django page that has a counter on it. I use
I have a simple form that I don't want accidentally submitted multiple times. I
I have a very simple use case. I have an xhr object of ids

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.