4

I am trying to get the following output. All rows and columns are text wrapped except the header though:

Excel screenshot

import pandas as pd
    import pandas.io.formats.style
    import os
    from pandas import ExcelWriter
    import numpy as np

    from xlsxwriter.utility import xl_rowcol_to_cell
    writer = pd.ExcelWriter('test1.xlsx',engine='xlsxwriter',options={'strings_to_numbers': True},date_format='mmmm dd yyyy')  
    df = pd.read_csv("D:\\Users\\u700216\\Desktop\\Reports\\CD_Counts.csv")
    df.to_excel(writer,sheet_name='Sheet1',startrow=1 , startcol=1, header=True, index=False, encoding='utf8')  
    workbook  = writer.book
    worksheet = writer.sheets['Sheet1']

    format = workbook.add_format()
    format1 = workbook.add_format({'bold': True, 'align' : 'left'})
    format.set_align('Center')
    format1.set_align('Center')
    format.set_text_wrap()
    format1.set_text_wrap()
    worksheet.set_row(0, 20, format1)
    worksheet.set_column('A:Z', 30, format)
    writer.save()

format is applied for all rows and columns except header. i dont know why format is not applied to first column (Header) or i would like to manually add column header numbers such as 0,1,2 etc so that i will turn of the header therefore all the rows and columns will be formatted

In the above screenshot wrap text is not applied to A1 to E1, C1 column has header with lot of space. if i manually click wrap text it gets aligned else all the header is not formatted using text wrap.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
prasi
  • 43
  • 1
  • 4
  • I suggest you edit the question to include a screenshot from Excel showing how you want the data to appear. – Martin Evans Aug 30 '17 at 08:55
  • The column format isn't applied to the the header cells in the first row since Pandas has already applied a cell format to those cells and that overrides the column format. See the [following workaround](https://stackoverflow.com/questions/36694313/pandas-xlsxwriter-format-header) in this SO question and the explanation. – jmcnamara Aug 30 '17 at 10:48
  • i tried pd.formats.format.header_style = None it throws error module 'pandas' has no attribute 'formats' – prasi Aug 30 '17 at 10:54
  • Then your version of Pandas may be older than 0.18.1. Try the older version shown in the linked answer: `pd.core.format.header_style = None` – jmcnamara Aug 30 '17 at 11:16
  • okay ! Thank u so much :) – prasi Aug 30 '17 at 11:21

1 Answers1

3

A couple of problems:

  1. Your code is correctly attempting to format the header, but when you create your file using .to_excel() you are telling it to start at row/col 1, 1. The cells though are numbered from 0, 0. So if you change to:

    df.to_excel(writer,sheet_name='Sheet1', startrow=0, startcol=0, header=True, index=False, encoding='utf8')  
    

    You will see col A and row 1 are both formatted:

    excel screenshot

    i.e. Col A is 0 and Row 1 is 0

  2. When using Pandas to write the header, it applies its own format which will overwrite the formatting you have provided. To get around this, turn off headers and get it to only write the data from row 1 onwards and write the header manually.

The following might be a bit clearer:

import pandas as pd
import pandas.io.formats.style
import os
from pandas import ExcelWriter
import numpy as np

from xlsxwriter.utility import xl_rowcol_to_cell

writer = pd.ExcelWriter('test1.xlsx', engine='xlsxwriter', options={'strings_to_numbers': True}, date_format='mmmm dd yyyy')  
#df = pd.read_csv("D:\\Users\\u700216\\Desktop\\Reports\\CD_Counts.csv")
df = pd.read_csv("CD_Counts.csv")
df.to_excel(writer, sheet_name='Sheet1', startrow=1 , startcol=0, header=False, index=False, encoding='utf8')  
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

format_header = workbook.add_format()
format_header.set_align('center')
format_header.set_bold()
format_header.set_text_wrap()
format_header.set_border()

format_data = workbook.add_format()
format_data.set_align('center')
format_data.set_text_wrap()

worksheet.set_column('A:Z', 20, format_data)
worksheet.set_row(0, 40, format_header)

# Write the header manually
for colx, value in enumerate(df.columns.values):
    worksheet.write(0, colx, value)

writer.save()

Which would give you:

header with text wrap from pandas

Note: It is also possible to tell Pandas the style to use, or to force it to None so it will inherit your own style. The only drawback with that approach is that the method required to do that depends on the version of Pandas that is being used. This approach works for all versions.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
  • Col A is 0 and Row 1 is 0 doesnt work :( center alignment is working but text wrap is not working. in C1 row there is a header which has lot of space and it doesnt wrap the text :( – prasi Aug 30 '17 at 09:50
  • Please add a screenshot of what you are trying to get in your question (i.e. manually make a correct looking one in Excel). – Martin Evans Aug 30 '17 at 09:52
  • I have added a screenshot. please look into it – prasi Aug 30 '17 at 10:04
  • I have updated the script to solve the problem with text wrap on the header row. – Martin Evans Aug 30 '17 at 10:54
  • In excel for dates am getting Text date with 2 digit error! is is possible to resolve that ? – prasi Aug 30 '17 at 11:10
  • Glad it worked. Regarding your dates, you will need to make sure the format in your dataframe is correct (i.e. not just strings). You might want to consider starting another question for this. – Martin Evans Aug 30 '17 at 11:12
  • Great ! Thank u so much for ur help :) – prasi Aug 30 '17 at 11:20
  • @MartinEvans I think that you are right that this is a better, version independent, approach to the solving this frequently asked question. So I've added it to the [examples](https://xlsxwriter.readthedocs.io/example_pandas_header_format.html#ex-pandas-header-format) and [docs](https://xlsxwriter.readthedocs.io/working_with_pandas.html#formatting-of-the-dataframe-headers). Thanks! – jmcnamara Sep 03 '17 at 19:11