Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88854

setting column to num format text instead of general python 3

$
0
0

I was able to change the header to text as the columns but I'm getting a strange error.

Now it still returns NaN but if I got in and manually highlight column B (the vendor name in text) and change the column to text via excel suddenly the second script works perfectly. And I only have to change worksheet 1.

Is there a way to mimic that in python/xlsxwriter?

I have two python scripts. One will take data from an excel sheet and parse it out to separate worksheets depending on a grouping. The second reads the totals and returns a table for an email. I've been manually building this in excel and working on automating it. The second script that builds the html table works just fine with my manual sheet. The workbook I've built with xlsxwriter looks identical save for column B with vendor names is formatted as General instead of Text (the manual sheet is text). My second script now only shows NaN when looking at the newly built sheet. I've tried to set format to text but it's not working. I am able to set format for currency on a different column so I'm not sure why text isn't working.

When I look at the cells I can see cell showing amount which is the same data returns a number but the summed cell doesn't

I'm using python 3.6 and xlsxwriter.

Column B is the one I need to change, it's a vendor name.

worksheet_paygroup.set_column('B:B', 40)

I've searched through the docs for xlsxwriter and found something for 2.7 that was specific to this but doesn't seem to work with 3.

I've tried to set_column, write_column, add_format, add_num_format

import pandas as pd
import numpy as np
import os
import datetime as dt
import pandas.io.formats.excel

df = pd.read_excel(test_sheet)

#this grabs the data from different excel sheet
df_paygroup = df[df['payments'] == 'paygroup']
df_paygroup.drop(columns='unneeded_column')
df_paygroup.insert(3, 'Total', value='') #sets new column
df_paygroup = df_paygroup[['Vendor', 'Name', 'Amount', 'Total']]
df_paygroup = df_paygroup.sort_values(by=['Amount'], ascending=False)


writer = pd.ExcelWriter('new_sheet.xlsx', engine='xlsxwriter')
pandas.io.formats.excel.header_style = None

df_paygroup.to_excel(writer, sheet_name='paygroup', index=False)
#there are 15 of these with different names


workbook = writer.book
money_format = workbook.add_format({'num_format': '[$$-409]#,##0.00'})
text_format = workbook.add_format({'num_format': '@'})

worksheet_paygroup = writer.sheets['paygroup']
worksheet_paygroup.set_column('A:A', 11)
worksheet_paygroup.set_column('B:B', 40)
# worksheet_paygroup.write(text_format) $this returns error
worksheet_paygroup.set_column('C:C', 19, money_format)
worksheet_paygroup.set_column('D:D', 16)
worksheet_paygroup.write_formula('E1', '=SUM(C2:C743)')
worksheet_paygroup.set_column('E:E', 20, money_format)


writer.save()
writer.close()

output in second script should show the dollar value not nan.

With the new sheet it shows: nan np.isnan = True

the old file: 2265.50 (the correct amount) np.isnan = False

<tr>
                            <th>Pay Cycle</th>
                            <th>Description</th>
                            <th>Pay Cycle Amount</th>
                            <th>Daily Total</th>
                        </tr>
                        <tr>
                            <td>paygroup</td>
                            <td>Description</td>
                            **<td>$nan</td>**
                            <td> </td>
                        </tr>

Viewing all articles
Browse latest Browse all 88854


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>