简体   繁体   中英

Exporting the data to Excel

Here is my code:

import xlwt
file = 'C://Users/MM/Desktop/'
wb = xlwt.Workbook()
sh = wb.add_sheet('S1')

d = [['Numbers','4,849,377','736,732','6,731,484']['Money','$43,253','$70,760','$49,774'],['Decnum','1.22','3.45','9.10']]

for row, nums in enumerate(d):
    for col, value in enumerate(nums):
        sh.write(col, row , value)

wb.save(file +'try.xls')
print('Exported')

Following are the things I'm trying to accomplish:

1) Everything in first row needs to be bold and in italics.

2) The width for all the columns needs to be same(fixed).

3) The 'Numbers','Money', and 'Decnum' gives me an error in excel telling me to convert them into numbers with green error arrows.

4) The data needs to centered.

Here is an example of the outlook

<IMG> http://i59.tinypic.com/dgu48.jpg <IMG>

You are looking for replace()

>>> x = '1,2,3'
>>> x.replace(',', '')
'123'

for row, nums in enumerate(d):
    for col, value in enumerate(nums):
        try:
            value = int(value.replace(',', ''))
        except:
            pass
        sh.write(col, row , value)

Style formatting:

style = xlwt.XFStyle()

font = xlwt.Font()
font.bold = True
style.font = font

sh.write(col, row , value, style=style)

Summarize:

style = xlwt.XFStyle()

font = xlwt.Font()
font.bold = True
style.font = font

for row, nums in enumerate(d):
    for col, value in enumerate(nums):
        try:
            value = int(value.replace(',', ''))
        except:
            pass
        if col == 0:
            sh.write(col, row , value, style=style)
        else:
            sh.write(col, row , value)

Another approach is to use easyxf() :

head = xlwt.easyxf('align: horiz center; font: bold 1,')
common = xlwt.easyxf('align: horiz center;')

for row, nums in enumerate(d):
    for col, value in enumerate(nums):
        try:
            value = int(value.replace(',', ''))
        except:
            pass
        if col == 0:
            sh.write(col, row, value, style=head)
        else:
            sh.write(col, row, value, style=common)

Widths & Heights

Add that bedore wb.save()

import itertools

col_width = 26 * 20   

try:
    for i in itertools.count():
        sh.col(i).width = col_width
except ValueError:
    pass

Setting cell format

Additional formats

num = xlwt.easyxf('align: horiz center;', '#,##')

if col == 0:
    sh.write(col, row, value, style=head)
else:
    if type(value) is int:
        sh.write(col, row, value, style=num)
    else:
        sh.write(col, row, value, style=common)

Full snippet:

import itertools
import xlwt

wb = xlwt.Workbook()
sh = wb.add_sheet('S1')

d = [['Numbers', '4,849,377', '736,732', '6,731,484'],
     ['Letters', 'a', 'b', 'c'], ['Colors', 'red', 'green', 'yellow']]


head = xlwt.easyxf('align: horiz center; font: bold 1,')
common = xlwt.easyxf('align: horiz center;')
num = xlwt.easyxf('align: horiz center;', '#,##')

for row, nums in enumerate(d):
    for col, value in enumerate(nums):
        try:
            value = int(value.replace(',', ''))
        except:
            pass
        if col == 0:
            sh.write(col, row, value, style=head)
        else:
            if type(value) is int:
                sh.write(col, row, value, style=num)
            else:
                sh.write(col, row, value, style=common)

    col_width = 200 * 20

    try:
        for i in itertools.count():
            sh.col(i).width = col_width
    except ValueError:
        pass

wb.save('tryx.xls')
print 'Exported'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM