Andrew Konstantinov
Andrew Konstantinov

Reputation: 63

Set worksheet.hide_gridlines(2) to certain range of cells

Im creating Excel file from pandas and I'm using worksheet.hide_gridlines(2) the problem that all gridlines are hide in my current worksheet.I need to hide a range of cells, for example A1:I80.How can I do that?

Upvotes: 3

Views: 3499

Answers (2)

patrickjlong1
patrickjlong1

Reputation: 3823

I've put together an answer that will remove gridlines for a particular row. You could modify it to do so for a range of cells.

This fully reproducible example would require you to download the pandas_datareader module for the example only.

import pandas as pd
import xlsxwriter
import pandas_datareader.data as web

#pull in your own dataframe and remove the next two lines
df = web.DataReader("AMZN", 'google')
df = df.tail(180)

writer = pd.ExcelWriter('Test.xlsx', engine='xlsxwriter') 
df.transpose().to_excel(writer, sheet_name='Sheet1') 
workbook  = writer.book 
ws = writer.sheets['Sheet1']

format1 = workbook.add_format({
'bg_color': 'white',
'pattern':1,
})

format2 = workbook.add_format({
'num_format': 'dd/mm/yy',
'bold': 1,
'text_wrap': 1,
})

ws.set_column(1, len(df), 30)
#if you're using a different df remove format2 - it's for a date
ws.set_row(0, 20, format2)
ws.write_row('B2', df['Open'], format1)


workbook.close()

In the output below, you can see the gridlines are removed for the second row. Second Row - Gridlines removes image

Upvotes: 1

jmcnamara
jmcnamara

Reputation: 41644

As far as I know that isn't possible in Excel to hide gridlines for a range. Gridlines are either on or off for the entire worksheet.

As a workaround you could turn the gridlines off and then add a border to each cell where you want them displayed.

As a first step you should figure out how you would do what you want to do in Excel and then apply that to an XlsxWriter program.

Upvotes: 1

Related Questions