Converting txt to xlsx while setting the cell property for number cells as number

自古美人都是妖i 提交于 2019-12-31 04:29:06

问题


Related question: Error in converting txt to xlsx using python

I have the following code which I revised thanks you Anand S Kumar.

import csv
import openpyxl

import sys


def convert(input_path, output_path):
    """
    Read a csv file (with no quoting), and save its contents in an excel file.
    """
    wb = openpyxl.Workbook()
    ws = wb.worksheets[0]

    with open(input_path) as f:
        reader = csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
        for row_index, row in enumerate(reader, 1):
            for col_index, value in enumerate(row, 1):
                ws.cell(row=row_index, column=col_index).value = value

    wb.save(output_path)


def main():
    try:
        input_path, output_path = sys.argv[1:]
    except ValueError:
        print 'Usage: python %s input_path output_path' % (sys.argv[0],)
    else:
        convert(input_path, output_path)


if __name__ == '__main__':
    main()

A problem with this is that this saves xlsx in a way that saves purely number-only cells as normal text.

So when I had to open the xlsx file manually using MS-Excel and then click "Convert to number".

Can this code convert txt to xlsx in a way that automatically sets the cell property as number, if the cell is purely number?


回答1:


I think the issue is that when you read data using csv module, you are reading in all strings. Example -

a.csv looks like -

1,2,3
3,4,5
4,5,6

Code And result -

>>> import csv
>>> with open('a.csv','r') as f:
...     reader = csv.reader(f)
...     for row in reader:
...             print(row)
...
['1', '2', '3']
['3', '4', '5']
['4', '5', '6']

And in your particular code, you are directly setting this value returned by the csv module to openpyxl , hence you are getting the strings, instead of numbers.

The best solution here would be that if you know which are the columns that you are expecting data to be an integer for, you can put a checking your code to convert those data to integer before setting it to excel . Example -

int_cols = set([2,4,5]) #This should be the list of all columns , 1 indexed, that contain integers.
with open(input_path) as f:
    reader = csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
    for row_index, row in enumerate(reader, 1):
        for col_index, value in enumerate(row, 1):
            if col_index in int_cols:
                 ws.cell(row=row_index, column=col_index).value = int(value)
            else:
                ws.cell(row=row_index, column=col_index).value = value

If there are floats, you can use similar logic for them , define a set of columns that are float, and then if the col_index is that column, convert value to float before saving.


If by the line -

Can this code convert txt to xlsx in a way that automatically sets the cell property as number, if the cell is purely number?

You mean you want to set it to number for all cells that are only digits (not even decimals) , then you can use a method like the below -

def int_or_str(x):
    try:
        return int(x)
    except ValueError:
        return x

Then in your code, you can change the line setting the value, to -

ws.cell(row=row_index, column=col_index).value = int_or_str(value)

Use float() in the above method, if you want to convert floats as well.




回答2:


openpyxl does support the guess_types parameter for workbooks which will convert strings to numbers if possible. Makes this kind of thing easier where there is no ambiguity. But you are generally best of managing the conversion yourself.




回答3:


There are two things that may be causing your issue:

  1. You can/should convert your value from CSV to int or float like this:

    ws.cell(row=row_index, column=col_index).value = int(value)  # or float(value)
    
  2. You are restrictive with your csv.reader; you should make sure that you really have tabs as delimiter or that your CSV is really not quoted.



来源:https://stackoverflow.com/questions/32216635/converting-txt-to-xlsx-while-setting-the-cell-property-for-number-cells-as-numbe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!