0

I'm using script from here convert-csv-to-xlsx to do the conversion with little modification (I add several arguments including 'strings_to_numbers': True ):

import os
import glob
import csv
from xlsxwriter.workbook import Workbook

for csvfile in glob.glob(os.path.join('.', '*.csv')):
    workbook = Workbook(csvfile[:-4] + '.xlsx',  {'constant_memory': True, 'strings_to_urls': False, 'strings_to_numbers': True})
    worksheet = workbook.add_worksheet()
    with open(csvfile, 'rt', encoding='utf8') as f:
        reader = csv.reader(f)
        for r, row in enumerate(reader):
            for c, col in enumerate(row):
                worksheet.write(r, c, col)
                print(r)   
    workbook.close()

Everything works fine but as I've added the above mentioned argument I'm getting all the numbers as numbers in my xlsx files. I need to keep one column (first) as a string as there are long numbers which convert (due to excel number length limitation) to something like this 1,04713E+18 example

Maybe i need to remove the argument and convert the needed columns from strings to numbers at the end. Is that possible?

Alice
  • 45
  • 8

1 Answers1

0

Based on Workbook documentation. strings_to_numbers converts string to float. There is no parameter that converts string to int. Therefore, you need to make conversion manually.

Instead of doing worksheet.write(r, c, col). You need to do worksheet.write_number(r, c, col). Also, you need to convert col to int(col).

Number can be checked by regexp or any other method.

Dmytro Chasovskyi
  • 3,209
  • 4
  • 40
  • 82