Python 2.7 xlsxwriter没有使用valign正确格式化数据

时间:2018-04-18 01:07:41

标签: python-2.7 xlsxwriter

我将水平和垂直对齐设置为居中,但只有前两列以数据为中心。

第三列似乎是使用“顶部”垂直对齐设置数据:

#!/usr/bin/env python

import xlsxwriter

# Create our spreadsheet.
workbook = xlsxwriter.Workbook('stats.xlsx')
worksheet = []

# Open our data file to extract data.
data = open('server_results','r')

# Create a bold centered font for our column headers.
format = workbook.add_format()
format.set_bold()
format.set_align('center')
format.set_align('vcenter')
format.set_text_wrap()

# Non-headers will be centered.
format1 = workbook.add_format()
format1.set_align('center')
format1.set_align('vcenter')
format1.set_text_wrap()

# Provider label/name for worksheet.
worksheet.append(workbook.add_worksheet('Stats'))

# Adjust column widths for data.
worksheet[0].set_column(0,2,25)
#worksheet[0].set_column(1,1,25)
#worksheet[0].set_column(1,2,25)
#worksheet[0].set_row(0,40)

# Start from the first cell. Rows and columns are zero indexed.
worksheet[0].write('A1', 'Column 1', format)
worksheet[0].write('B1', 'Column 2', format)
worksheet[0].write('C1', 'Column 3', format)

row = 1
col = 0

# Count lines
linelist = data.readlines()
count = len(linelist)

# Populate spreadsheet.
for num in range (0, count):
    line = linelist[num]
    splitline = line.split("\t")
    worksheet[0].write(row, col, splitline[0], format1)
    worksheet[0].write(row, col + 1, splitline[1], format1)
    worksheet[0].write(row, col + 2, splitline[2], format1)
    row += 1

#close workbook
workbook.close()
EOF

我尝试使用以下代码修改数据格式:

format = workbook.add_format({'bold': True, 'bg_color': 'yellow', 'border': 5, 'align': 'center', 'valign': 'middle', 'text_wrap': True})

但是当我这样做时,没有列的中心 - 使用这种格式,前两列是底部对齐的,第三列是顶部对齐的

2 个答案:

答案 0 :(得分:1)

  

第三栏似乎是设置了一个' top'垂直对齐

我使用一些示例数据运行了您的程序版本,但我没有看到这种行为:

import xlsxwriter

# Create our spreadsheet.
workbook = xlsxwriter.Workbook('stats.xlsx')
worksheet = []

# Create a bold centered font for our column headers.
format = workbook.add_format()
format.set_bold()
format.set_align('center')
format.set_align('vcenter')
format.set_text_wrap()

# Non-headers will be centered.
format1 = workbook.add_format()
format1.set_align('center')
format1.set_align('vcenter')
format1.set_text_wrap()

# Provider label/name for worksheet.
worksheet.append(workbook.add_worksheet('Stats'))

# Adjust column widths for data.
worksheet[0].set_column(0,2,25)

# Start from the first cell. Rows and columns are zero indexed.
worksheet[0].write('A1', 'Column 1', format)
worksheet[0].write('B1', 'Column 2', format)
worksheet[0].write('C1', 'Column 3', format)

row = 1
col = 0

# Count lines
linelist = ['foo\tbar\tbaz\tbing'] * 5
count = len(linelist)

# Populate spreadsheet.
for num in range (0, count):
    line = linelist[num]
    splitline = line.split("\t")
    worksheet[0].write(row, col, splitline[0], format1)
    worksheet[0].write(row, col + 1, splitline[1], format1)
    worksheet[0].write(row, col + 2, splitline[2], format1)
    row += 1

#close workbook
workbook.close()

输出显示了预期的水平和垂直居中:

enter image description here

你能否澄清一下这是什么问题。

答案 1 :(得分:1)

使用以下内容:

splitline = [l.rstrip() for l in line.split("\t")]

解决了这个问题。