1

I have tried the below code to create excel file with sheetnames. Sheets are created but empty. And one new sheet with only ws2 data is found. Can sonmeone help rewrite this.

workbook = openpyxl.Workbook()
ws1 = workbook.create_sheet('ONE')
ws2 = workbook.create_sheet('TWO')

ws1 = workbook.active
ws2 = workbook.active

# Construct test worksheets with some content.
ws1['A1'] = 'ID'
ws1['B1'] = 'Text'
ws1['C1'] = 'Category'

ws1['A2'] = '234'
ws1['B2'] = 'Sample'
ws1['C2'] = 'ASD'

ws2['A1'] = 'ID'
ws2['B1'] = 'Text'
ws2['C1'] = 'Category'

ws2['A2'] = '566'
ws2['B2'] = 'Sample'
ws2['C2'] = 'PED'

ws2['A3'] = '896'
ws2['B3'] = 'SAmple'
ws2['C3'] = 'HEAD'

workbook.save('sam.xlsx')
1
  • You set ws1 and ws2 to the same object. Commented Mar 1, 2021 at 9:53

2 Answers 2

5

When you create a Workbook object, it creates an Excel workbook with a default sheet Sheet. And executing workbook.active returns the active sheet of the Workbook object. (Which is Sheet in your case)

So both ws1 and ws2 are indicating the sheet Sheet for your case resulting from the lines;

ws1 = workbook.active
ws2 = workbook.active

That is why the code you wrote writes all the cells to Sheet.

And as the documentation of the openpyxl indicates, you can achive what you're trying by;

workbook = openpyxl.Workbook()

ws1 = 'ONE'
ws2 = 'TWO'

workbook.create_sheet(ws1)
workbook.create_sheet(ws2)

workbook[ws1]['A1'] = 'ID'
workbook[ws1]['B1'] = 'Text'
workbook[ws1]['C1'] = 'Category'
workbook[ws1]['A2'] = '234'
workbook[ws1]['B2'] = 'Sample'
workbook[ws1]['C2'] = 'ASD'

workbook[ws2]['A1'] = 'ID'
workbook[ws2]['B1'] = 'Text'
workbook[ws2]['C1'] = 'Category'
workbook[ws2]['A2'] = '566'
workbook[ws2]['B2'] = 'Sample'
workbook[ws2]['C2'] = 'PED'
workbook[ws2]['A3'] = '896'
workbook[ws2]['B3'] = 'SAmple'
workbook[ws2]['C3'] = 'HEAD'

# You can also call the line below to delete the sheet 'Sheet'
workbook.remove(workbook['Sheet'])

workbook.save('sam.xlsx')
Sign up to request clarification or add additional context in comments.

1 Comment

If this was what you were looking for, you can mark this answer as the answer of this question, cheers!
2

The first sheet will be created once workbook.active is called.

workbook = openpyxl.Workbook()

ws1 = workbook.active
ws1.title = 'ONE'
ws2 = workbook.create_sheet('TWO')
# Construct test worksheets with some content.
ws1['A1'] = 'ID'
ws1['B1'] = 'Text'
ws1['C1'] = 'Category'

ws1['A2'] = '234'
ws1['B2'] = 'Sample'
ws1['C2'] = 'ASD'

ws2['A1'] = 'ID'
ws2['B1'] = 'Text'
ws2['C1'] = 'Category'

ws2['A2'] = '566'
ws2['B2'] = 'Sample'
ws2['C2'] = 'PED'

ws2['A3'] = '896'
ws2['B3'] = 'SAmple'
ws2['C3'] = 'HEAD'

workbook.save('sam.xlsx')

1 Comment

Official doc: A workbook is always created with at least one worksheet. You can get it by using the Workbook.active.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.