Reading csv file and writing the df to excel with text wrap

和自甴很熟 提交于 2019-12-05 18:08:11

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:

    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:

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!