Update: So I just went back to excel: Convert the D column ( amount) to a number and then went to format cells and manually chose 2 decimal places and it worked fine, the text version looked perfectly aligned. However, my question is how can I automatically save the column to convert it's contents to a number with 2 decimal places. I know I am returning back to my first question but I believe it's the closest question to my problem I can think of.
I am using python to create an excel sheet that I would later convert to a text file. I have specific requirements for the width and alignment. If it's slightly changed Ellucian ( a software) will not be able to read it. For now, I am struggling to right-align the column of amount column: D . I used xlwt to right-align it. It seemed to work until I converted it to a .prn file, then it left-aligned again.
Find the code below:
import pandas as pd
import xlwt
df = pd.read_excel("tms_report.xls", "Disbursement Detail Report")
workbook = xlwt.Workbook(encoding="ascii")
worksheet = workbook.add_sheet("Sheet", cell_overwrite_ok = False)
#Varibales: Width of the column, alignement
right_align = xlwt.easyxf("align: vert centre, horiz right")
def adjustement(data):
x = len(data)
del data[x - 1]
return data
arrStudentID = adjustement(df['Student ID'])
arrStudentFirst = adjustement(df['Student First Name'])
arrStudentLast = adjustement(df['Student Last Name'])
arrAmount = adjustement(df['Payment Amount'])
worksheet.col(0).width = 256*10
worksheet.col(1).width = 256*17
worksheet.col(2).width = 256*18
worksheet.col(3).width = 256*8
i = 0
for studentID in arrStudentID:
if len(studentID) < 7:
studentID = "0" + studentID
while len(studentID) >= 8:
studentID = studentID[1:]
worksheet.write(i, 0, studentID)
i+=1
i = 0
for studentFirst in arrStudentFirst:
worksheet.write(i, 1, studentFirst)
i+=1
i = 0
for studentLast in arrStudentLast:
worksheet.write(i, 2, studentLast)
i+=1
#This is the part that I need to be a number or right aligned
i = 0
for amount in arrAmount:
amount = '%.2f'%amount
worksheet.write(i, 3, amount, right_align)
i += 1
workbook.save("tms_import.xls")
The excel file that I receive looks perfectly like I want. But when I save it as a .prn file the right alignment of the amount column is messed up.
Also, this is an updated question, since I first thought that by converting the float into an actual number would solve my question. But thanks to the answers of users here I figured that my problem is as mentioned above. I truly appreciate your help!!