Python 将数据导出到Excel
这是我的密码:Python 将数据导出到Excel,python,excel,formatting,Python,Excel,Formatting,这是我的密码: 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)
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')
以下是我正在努力完成的事情:
1) 第一行中的所有内容都需要粗体和斜体
2) 所有列的宽度都需要相同(固定)
3) “number”、“Money”和“Decnum”在excel中给了我一个错误,告诉我将它们转换成带有绿色错误箭头的数字
4) 数据需要集中
下面是outlook的一个示例
您正在寻找的
样式格式:
style = xlwt.XFStyle()
font = xlwt.Font()
font.bold = True
style.font = font
sh.write(col, row , value, style=style)
总结:
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)
另一种方法是使用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)
宽度和高度
添加bedorewb.save()
设置单元格格式
完整片段:
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'
可以我得到的第一行有粗体文本。但是,我不想替换逗号。我需要保留它们。还有,斜体字、列宽和数据居中如何?正如我所说,它应该看起来像图像中的数据。@user3990145:除了加宽列之外,我得到了一切。我使用了代码的第二种方法;有easyxf的那个。您能告诉我如何将这些列加宽到一定的宽度吗?@user5403501:如果可以的话,最后一件事是。可以在
value=int(value.replace(',','')
行中,我将'
替换为','
,因此现在我得到了逗号。然而,仍然存在一个问题,那就是在excel中,它告诉我“数字存储为文本”,并给我一个角落中绿色箭头的错误。如何将它们存储为数字?图像中错误的链接:@user3990145:您是否仍在尝试修复绿色箭头问题?@user5403501value=int(value.replace(',','')
如果删除逗号并将其转换为int,则不会出现任何错误。因此,对于Excell设置,请检查
import itertools
col_width = 26 * 20
try:
for i in itertools.count():
sh.col(i).width = col_width
except ValueError:
pass
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)
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'