I am trying to read the excel file (.xlsb format) using pyxlsb library in python and then convert that excel file to csv. But while reading the excel file the cells containing date/ datetime value are unable to preserve the format. Its getting converted into integer format.
Python code
import pyxlsb
from datetime import datetime
import csv
from decimal import Decimal
def process_excel_xlsb(excelfile_path, outputCSV_path):
workbook = pyxlsb.open_workbook(excelfile_path)
sheet = workbook.get_sheet('Sheet1')
with open (outputCSV_path, 'w', newline='') as csvfile:
writer csv.writer (csvfile, delimiter=';', quoting=csv.QUOTE_MINIMAL, lineterminator='n')
for rows in sheet.rows():
row_data[]
for cell in rows:
cell_value = cell.v
#print(cell_value)
if isinstance(cell.v, float)
decimal_vl = Decimal(str(cell.v))
row data.append(decimal_vl)
else:
row_data.append(cell_value)
writer.writerow(row_data)
print('CSV created at {outputCSV path}')
excelfile path = 'my_file.xlsb'
outputCSV path = 'new_report CSV.csv'
process_excel_xlsb(excelfile path, outputCSV path)
Excel sample data
| S_no | Date | Float | Date_time |
| ----- | -------- | ------- | ----------------- |
| 111 | 7/6/2018 | 111.999 | 7/31/88 1:00 AM |
| 222 | 1/10/2020 | 222.999 | 9/22/86 1:00 AM |
Current conversion
S_no ; Date ; Float ; Date_time
111.0;43287.0;111.999;32355.041666666668
222.0;43475.0;222.999;31677.041666666668
I even referred to this solution from stackoverflow formatting-date-to-mm-dd-yyy-from-xlsb-file
But its also converting int/ decimal values to date.
I am expecting the created csv file to preserve all the integers, decimal, date format and date time format(YYYY-mm-dd).
Can some one please guide me on this.
nid101 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.