Python Forum
openpyxl rename sheet acting strange
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
openpyxl rename sheet acting strange
#1
I have a big sales Excel file. I want to extract data according to a criteria and save it to another Excel

When I make the new Excel, it has 1 sheet called Sheet. So I want to rename that.

I get a tuple with the name of the column I want to sort by, in this case column company_name.

I get the name of the company I want to find in any row, in this case vjCoQ Ltd.

I get the column number for this column, in this case 3.

So mytup = ('company_name', 'vjCoQ Ltd.', 3)

When I do this, I get an error and I don't know why. The tsheet 'vjCoQ Ltd.' is clearly there but I get:

Quote:KeyError: 'Worksheet <Worksheet "vjCoQ Ltd."> does not exist.'

Can anyone see the problem? This is using Idle:

Quote:>>> targetFile = openpyxl.Workbook()
>>> tsheet = targetFile.active
>>> tsheet
<Worksheet "Sheet">
>>> tsheet.title = mytup[1]
>>> tsheets = targetFile.sheetnames
>>> tsheets
['vjCoQ Ltd.']
>>> maxCol = sourceFile[sheet].max_column
>>> tsheet
<Worksheet "vjCoQ Ltd.">
>>> for colNum in range(1, maxCol + 1):
sv = sourceFile[sheet].cell(row=1, column=colNum).value
targetFile[tsheet].cell(row=1, column=colNum).value=sv


Traceback (most recent call last):
File "<pyshell#24>", line 3, in <module>
targetFile[tsheet].cell(row=1, column=colNum).value=sv
File "/home/pedro/.local/lib/python3.6/site-packages/openpyxl/workbook/workbook.py", line 273, in __getitem__
raise KeyError("Worksheet {0} does not exist.".format(key))
KeyError: 'Worksheet <Worksheet "vjCoQ Ltd."> does not exist.'
>>>

The only way I found to get around that is get the sheet names again and set tsheet = tsheets[0]

tsheets = targetFile.sheetnames
# this works
tsheet = tsheets[0]   
So the whole function looks like this and works:

def makeXL(mytup):
        print('Now making the taget file ... ')
        # open a target file to save the data we want
        # make a new XL
        targetFile = openpyxl.Workbook()
        # a new wb only has 1 sheet called Sheet
        tsheet = targetFile.active
        # this doesn't work
        tsheet.title = mytup[1]
        tsheets = targetFile.sheetnames
        # this works
        tsheet = tsheets[0]        
        maxCol = sourceFile[sheet].max_column
        # first write the headers to target file
        for colNum in range(1, maxCol + 1):        
            sv = sourceFile[sheet].cell(row=1, column=colNum).value
            targetFile[tsheet].cell(row=1, column=colNum).value=sv
        savename = path2XL + mytup[1] + '_customers.xlsx'
        targetFile.save(savename)
        print('target file made and headers inserted ... ')
Reply
#2
tsheet is Worksheet instance. I believe it expects string as key, not sheet object.

Also I think, instead of targetFile[tsheet] you can use directly tsheet

tsheet.cell(row=1, column=colNum).value=sv
Pedroski55 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  openpyxl issue - How to reset sheet.ins_row to the real last row ... ejwjohn 6 793 Nov-08-2023, 01:19 PM
Last Post: ejwjohn
  breaking a large program into functions, not acting as expected Zane217 9 3,027 Sep-18-2021, 12:37 AM
Last Post: Zane217
  Nested dictionary acting strange Pedroski55 2 2,119 May-13-2021, 10:37 PM
Last Post: Pedroski55
  Append excel sheet using openpyxl TammyP 1 2,399 Feb-02-2021, 06:32 PM
Last Post: nilamo
  Excel: Apply formating of a sheet(file1) to another sheet(file2) lowermoon 1 2,058 May-26-2020, 07:57 AM
Last Post: buran
  Importing acting weird IILawrenceII 3 2,009 Jan-24-2020, 09:17 PM
Last Post: Marbelous
  Openpyxl - When save existing xlsx sheet, images/drawing does not get saved shubhamjainj 2 8,485 Apr-16-2019, 07:09 AM
Last Post: shubhamjainj
  inserting photos in 1 sheet of a 6 sheet excel file fails Pedroski55 0 2,382 Mar-03-2018, 01:53 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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