Python Forum
Unable to write to excel - Using openpyxl module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to write to excel - Using openpyxl module
#1
Hi there,

Trying to write simple class for writing data in to excel file.

first create workbook and saved to the given file name

   from openpyxl import Workbook()
   class Excel:
      wb = None
      ws = None
1)Initialize the excel file . Successfully create the excel file and managed to save to file location
def initexcel(self,sheet_name)
		wb = Workbook()
		ws = wb.create_sheet(sheet_name,0)
		wb.save(filename)
        return ws


Problem starting from this point
2) write data into excel file.
     def writeexcel(sheet_name, ro,col,data)
        ws = self.wb[sheet_name]             # trying to activate the sheet using the sheet name. N not working  
        ws.cell(column = col,row = ro, value = data #not working
What I need to do is to write data to excel file ( that have created using the first method initexcel). activate the sheet_name sheet and write data to cell ( ro,col)

Thanks in advance!
Appreciate any help Smile
Reply
#2
In your writeexcel function/method, use
wb = ws.active
to specify the active workbook.

See https://openpyxl.readthedocs.io/en/stable/usage.html
Reply
#3
Thanks for the reply!

I have tried it two different way

1) getting error
   def writeexcel(sheet_name,to,col,data)
       ws  = self.wb[sheet_name] #calling sheet by sheet_name
                                 #TypeError: 'NoneType' object is not subscriptable
       ws = self.active          #AttributeError:'NoneType' object has no attribute active 
2)second method not getting any error but it's not writing in to excel
  def writeexcel(self,wkb,sheet_name,to,col,data):
     print(wkb[sheet_name]) #<Worksheet 'sheet1'>
     ws =wkb[sheet_name]
     ws = wkb.active
     ws[’A1] = 'abc’ 
This method able to run without any error, but writing not happening
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  trouble reading string/module from excel as a list popular_dog 0 386 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Openpyxl module breaking my code EnderSM 5 986 May-26-2023, 07:26 PM
Last Post: snippsat
  Excel Automation using Openpyxl pradyumnajpn 1 771 May-16-2023, 09:38 AM
Last Post: Larz60+
  How do I write all csv records to Excel ? Revox 2 915 Mar-29-2023, 03:53 PM
Last Post: deanhystad
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,048 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,525 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  Openpyxl manipulate excel write formula SamLiu 0 1,002 Nov-04-2022, 03:00 PM
Last Post: SamLiu
  dataframe write to tab delimited differs from Excel koh 0 1,959 Aug-01-2021, 02:46 AM
Last Post: koh
  Read and write active Excel file euras 4 3,383 Jun-29-2021, 11:16 PM
Last Post: Pedroski55
  How to append a value to specific excel cell using openpyxl hobbyist 0 4,768 Mar-05-2021, 07:14 PM
Last Post: hobbyist

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020