0

I'm trying to use the openpyxl library (see https://openpyxl.readthedocs.io/) to create some basic Excel-type files for use in Excel and Planmaker (from Softmaker FreeOffice 2018). The code I'm using is basically the same as shown on the openpyxl library web site, viz:-

from openpyxl import Workbook
import csv

Excel_file = "Test.xlsx"
csv_file   = "test.csv"

# ----

wb = Workbook()
ws = wb.active

with open(csv_file, 'r') as fp:
   for row in csv.reader(fp):
      ws.append(row)  # This creates cells that aren't numbers OR formattable?
# endfile

wb.save(Excel_file)

exit(0)         

...and the .csv file is:-

Fruit,Quantity
Apples,222.09
Oranges,1032.12
Bananas,14.12

The file this code creates is Ok... but the numeric cells are formatted as 'Default' (they appear left-justified, not right-justified, as is normal with numeric fields) and can't seem to be formatted any specific way. The contents are 'Default' justified, etc. If I set the cells to 'Centred', the display of the cell is changed to centre the numeric text; however, if I then try to assign a 'Number' format (say, with 2 decimal places and to use commas for thousands), the display is not updated with the decimal places nor the thousands separator and the displayed text becomes left-justified, as it was before I changed it to 'Centred'.

I'm not sure if the problem is with Python, the library, or the Planmaker application (I don't have access to Excel at present), so I'm not sure where to go to see about resolving the problem. I'm not using 'pandas' or other such tools as I'm only dealing with small amounts of data and I want to keep the Python code small and 'lightweight' as I can.

I'm using Python 3.7.0 (collected from python.org some months ago, installed for everyone -- that is, not a 'user' installation), Windows 8.1 32-bit, openpyxl 3.0.2 and FreeOffice Planmaker 2018 Rev 973.1103 32-bit.

An example of the 'test.xlsx' file created by Python/openpyxl is located at: http://www.filedropper.com/test_20160 .

I'd appreciate any thoughts on what might be going awry.

Thanks.

1 Answer 1

1

I tried to open a file in Excel - yes, the formatting is default:

enter image description here

You need to change a cell style for the numeric cells OR convert a value to desired python type before appending.

with open(csv_file, 'r') as fp:
    reader = csv.reader(fp)
    header = next(reader)
    ws.append(header)

    for row in reader:
        ws.append((row[0], float(row[1])))

Or, alternatively (see the docs for more details):

for row in range(2, rows_num):
    ws["B{}".format(row)].value = float(ws["B{}".format(row)].value)
    ws["B{}".format(row)].number_format = '#,##0.00'
1
  • 1
    Ok, thanks. It makes sense, I guess, as each field in each CSV record is 'text', hence, will go into the worksheet as text. I've been 'spoilt'(!) by too many years of Perl, where the usage often dictates the 'type'. Using a similar function in Perl will realize the sequence of digits and a dot is a float and insert it as such into the worksheet; Python is a bit more demanding (which has pros and cons). A good Python lesson learnt, thanks.
    – Skeeve
    Commented Dec 27, 2019 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.