Python Forum
Changing the initial worksheet name in an MS Excel file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing the initial worksheet name in an MS Excel file
#1
In the following code I want the initial sheet name to change from 'Sheet' to some other name. What code can I use to do this. Is there a way of making the initial sheet to a nane other than 'Sheet' from the beginning without even changing it afterwards?

from openpyxl import Workbook

# This function will create a default MS Excel file.  
def create_excel_file(excel_file_name):

    # creates Workbook object.
    wb = Workbook()

    # invoke Workbook.active property to create the initial worksheet i.e. ‘Sheet’
    initial_work_sheet = wb.active

    # workbook has an initial worksheet.
    initial_sheet_names = wb.sheetnames
    print(initial_sheet_names)
Reply
#2
It's OK folks I worked it out.

initial_work_sheet.title ="New_Name"
Reply
#3
Just use sheet.title = 'a name for my sheet'

from openpyxl import Workbook
path2XL = '/home/pedro/myPython/openpyxl/xlsx_files/'
# This function will create a default MS Excel file.  
def create_excel_file(excel_file_name): 
    # creates Workbook object.
    wb = Workbook() 
    # invoke Workbook.active property to create the initial worksheet i.e. ‘Sheet’
    initial_work_sheet = wb.active
    initial_work_sheet.title = 'my shiny new sheet'
    # workbook has an initial worksheet.
    initial_sheet_names = wb.sheetnames    
    print(initial_sheet_names)
    sheet = initial_sheet_names[0]
    # make sure you can write to it
    for colNum in range(1, 5):
        wb[sheet].cell(row=1, column=colNum).value='some data'
    wb.save(path2XL + 'shiny_newXL.xlsx')
Reply
#4
Many thanks Pedroski55.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 317 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 423 Feb-07-2024, 12:24 PM
Last Post: Viento
  Search Excel File with a list of values huzzug 4 1,216 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 822 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,089 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,018 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 805 Feb-16-2023, 08:11 PM
Last Post: cubangt
  Import XML file directly into Excel spreadsheet demdej 0 838 Jan-24-2023, 02:48 PM
Last Post: demdej
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,902 Jan-21-2023, 06:57 AM
Last Post: jacklee26
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,111 Dec-15-2022, 04:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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