Other Properties and Methods Using OpenPyXL

Method

>>> ws.title   # The name of the worksheet

‘Sheet’

>>> ws.sheet_state   # The visibility state

‘visible’

>>> ws.dimensions  # The size of the area containing data in the table

‘A2:G10’

>>> ws.sheet_properties  # Properties related to the worksheet, including tabColor, tagname, etc.

>>> ws.sheet_properties.tabColor=’FF0000′   # Set the background color of the tab label

>>> ws.active_cell     # The active cell

‘C9’

>>> ws.selected_cell    # The selected cell

‘C9’

Sample Code

#Other Worksheet Properties and Methods

#Import load_workbook function
from openpyxl import Workbook

#Create a new workbook
wb=Workbook()
#Get the active worksheet
ws=wb.active

#Worksheet title
print(ws.title)
#Visibility status
print(ws.sheet_state)
#Size of the portion of the table that contains data
print(ws.dimensions)
#Worksheet properties including tabColor, tagname etc.
print(ws.sheet_properties)
#Set the background color of the tab label
print(ws.sheet_properties.tabColor='FF0000')
#Active cell
print(ws.active_cell)
#Selected cells
print(ws.selected_cell)

wb.save('test.xlsx')
wb.close()

Leave a Reply

Your email address will not be published. Required fields are marked *