7

I have gone through many posts but did not found the exact way to do the below. Sorry for attaching screenshot(Just for better visibility) as well , I will write it also. Basically it looks like -

Name_of_the_Man Address_of_Man  City
Jordan           NC             LMN

Input csv looks like

Available csv

Output Needed Need this

I have this code with me that picks the csv and attach as sheet in excel.

writer = pd.ExcelWriter('final.xlsx'), engine='xlsxwriter')
for f in glob.glob(os.path.join(Path, "*.csv")):
         df = pd.read_csv(f)
         df.to_excel(writer, sheet_name=os.path.basename(f))
writer.save()

I want my csv file - having good space in between and color for the column header.I have went through this link Python - change header color of dataframe and save it to excel file but it's not serving the purpose - It is coloring the sheet itself apart from column.

Update: Got the answer below . Also wondering if that can be possible just a thought enter image description here

RonyA
  • 585
  • 3
  • 11
  • 26

1 Answers1

10

You can use Pandas Excel output with user defined header format with solution for change width by content:

writer = pd.ExcelWriter("file.xlsx", engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object. Note that we turn off
# the default header and skip one row to allow us to insert a user defined
# header. Also remove index values by index=False
df.to_excel(writer, sheet_name='Sheet1', startrow=1, header=False, index=False)

workbook  = writer.book
worksheet = writer.sheets['Sheet1']
# Add a header format.
header_format = workbook.add_format({
    'bold': True,
    'fg_color': '#ffcccc',
    'border': 1})
for col_num, value in enumerate(df.columns.values):
    worksheet.write(0, col_num, value, header_format)

    column_len = df[value].astype(str).str.len().max()
    # Setting the length if the column header is larger
    # than the max column value length
    column_len = max(column_len, len(value)) + 3
    print(column_len)
    # set the column length
    worksheet.set_column(col_num, col_num, column_len)

# Close the Pandas Excel writer and output the Excel file.
writer.save()

pic

Changed your solution:

writer = pd.ExcelWriter('final.xlsx'), engine='xlsxwriter')
for f in glob.glob(os.path.join(Path, "*.csv")):
         df = pd.read_csv(f)
         df.to_excel(writer, sheet_name=os.path.basename(f))

        workbook  = writer.book
        worksheet = writer.sheets[os.path.basename(f)]
        # Add a header format.
        header_format = workbook.add_format({
            'bold': True,
            'fg_color': '#ffcccc',
            'border': 1})
        for col_num, value in enumerate(df.columns.values):
            worksheet.write(0, col_num, value, header_format)
            column_len = df[value].astype(str).str.len().max()
            # Setting the length if the column header is larger
            # than the max column value length
            column_len = max(column_len, len(value)) + 3
            print(column_len)
            # set the column length
            worksheet.set_column(col_num, col_num, column_len)

writer.save()

EDIT:

writer = pd.ExcelWriter("file.xlsx", engine='xlsxwriter')

#skip 2 rows
df.to_excel(writer, sheet_name='Sheet1', startrow=2, header=False, index=False)

workbook  = writer.book
worksheet = writer.sheets['Sheet1']
# Add a header format.
header_format = workbook.add_format({
    'bold': True,
    'fg_color': '#ffcccc',
    'border': 1})

#create dictionary for map length of columns 
d = dict(zip(range(25), list(string.ascii_uppercase)))
#print (d)

max_len = d[len(df.columns) - 1]
print (max_len)
#C
#dynamically set merged columns in first row
worksheet.merge_range('A1:' + max_len + '1', 'This Sheet is for Personal Details')

for col_num, value in enumerate(df.columns.values):
    #write to second row
    worksheet.write(1, col_num, value, header_format)

    column_len = df[value].astype(str).str.len().max()
    column_len = max(column_len, len(value)) + 3
    worksheet.set_column(col_num, col_num, column_len)

# Close the Pandas Excel writer and output the Excel file.
writer.save()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Yeah, its working fine. I am thinking if we can add some Bold text above the column header like , ;'This Sheet is for this this thing'? and make put the csv from second row , just a thought ? – RonyA Jun 03 '18 at 11:27
  • Attached one screenshot at the end of the question . Please check if that can be done. Sorry for asking more help .I did not found that kind of thing while searching. – RonyA Jun 03 '18 at 11:34
  • 1
    @RishuA - Give me some time, do you need colspans for first row? – jezrael Jun 03 '18 at 11:44
  • I want some text to be entered above the column - that is for the description of the csv data. Please check the question for the last screenshot. I will put some variable in between the text to make it more precise . But want a way to do that . – RonyA Jun 03 '18 at 11:46
  • It’s not working somehow . Cab moved to second row as startrow set to 2 but the column headers are missing now . Only first column header is visible and in the first row itself . – RonyA Jun 03 '18 at 12:39
  • do you change `worksheet.write(0, col_num, value, header_format)` to `worksheet.write(1, col_num, value, header_format)` – jezrael Jun 03 '18 at 12:41
  • Give me sometime will check . Laptop battery got drained . – RonyA Jun 03 '18 at 12:42
  • @RishuA - I try remove all unnecessary commnets in last paragraph. there are comments only for changed code. – jezrael Jun 03 '18 at 12:43
  • 1
    I would say thanks for putting those comments . I am learning excel with pandas and those are helpful things – RonyA Jun 03 '18 at 12:44
  • 1
    I only remove it in last paragraph for easy finf differences with before :) Sure, best is not remove original comments. – jezrael Jun 03 '18 at 12:45
  • It’s working like a charm but the text did not converted to bold or in color . I will check the document how it can be done . – RonyA Jun 03 '18 at 12:57
  • @RishuA - You are welcome! btw, I use [this](http://xlsxwriter.readthedocs.io/example_merge1.html) as source of idea. – jezrael Jun 03 '18 at 12:58
  • 1
    Thank you for the link. Going to read now. Extremely thankful today . – RonyA Jun 03 '18 at 12:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172463/discussion-between-rishua-and-jezrael). – RonyA Jun 05 '18 at 03:47
  • Is it possible to club the multiple last picture output in a single excel sheet ? This isn't working- got this from some of your post. Its not capturing the complete data that inclues the colspan,column header for the 2nd csv and so on `dfs=[] writer = pd.ExcelWriter('Final.xlsx', engine='xlsxwriter') for excel in glob.glob(os.path.join(os.getcwd(), "*.csv")): df=pd.read_csv(excel) dfs.append(df) df.to_excel(writer) writer.save()` – RonyA Jun 05 '18 at 11:10
  • how can we use this code inorder to add color to column header of multiple dataframes and write them to excel? – be_real Apr 12 '23 at 10:45