xlsxwriter + Python循环遍历块以进行格式化

时间:2018-01-26 11:33:19

标签: python python-2.7 xlsxwriter

所以我试图创建一个电子表格(.xlsx)来获得一些布朗尼点。我没有使用过很多python,但是我慢慢地接受它。

我想问一下我的代码中某个部分的问题,请...

在下面的一堆写入中,我将以下信息放入电子表格中的单元格中。这就像一个魅力,但当我用“粗体”格式化一些单元格时或者'强调'使用xlsxwriter我很茫然。

# This is the template of the xlsx file
worksheet2.write('A2', 'Scan Creation Date:')
worksheet2.write('A3', 'Total Hosts Scanned:')
worksheet2.write('A5', 'OVERALL FIGURES')
worksheet2.write('A6', 'Total Sev 5:')
worksheet2.write('A7', 'Total Sev 4:')
worksheet2.write('A8', 'Total Sev 3:')
worksheet2.write('A9', 'Total Sev 2:')
worksheet2.write('A10', 'Total Sev 1:')
worksheet2.write('A11', 'Overall Total:')
worksheet2.write('A13', 'Hostname')
worksheet2.write('B13', 'IP Address')
worksheet2.write('C13', 'Sev 5')
worksheet2.write('D13', 'Sev 4')
worksheet2.write('E13', 'Sev 3')
worksheet2.write('F13', 'Sev 2')
worksheet2.write('G13', 'Sev 1')
worksheet2.write('H13', 'Total')
worksheet2.write('I13', 'Stats (%)')

我想要做的是反复重复使用上面的内容,但是使用粗体或下划线或者我需要做的任何内容附加到每一行,以使其在代码中看起来很漂亮。

# This is the template of the xlsx file
worksheet2.write('A2', 'Scan Creation Date:', bold)
worksheet2.write('A3', 'Total Hosts Scanned:', bold)
worksheet2.write('A5', 'OVERALL FIGURES', bold)
worksheet2.write('A6', 'Total Sev 5:', bold)
worksheet2.write('A7', 'Total Sev 4:')
worksheet2.write('A8', 'Total Sev 3:')
worksheet2.write('A9', 'Total Sev 2:')
worksheet2.write('A10', 'Total Sev 1:')
worksheet2.write('A11', 'Overall Total:', bold)
worksheet2.write('A13', 'Hostname', bold)
worksheet2.write('B13', 'IP Address')
worksheet2.write('C13', 'Sev 5', bold)
worksheet2.write('D13', 'Sev 4')
worksheet2.write('E13', 'Sev 3')
worksheet2.write('F13', 'Sev 2')
worksheet2.write('G13', 'Sev 1')
worksheet2.write('H13', 'Total')
worksheet2.write('I13', 'Stats (%)', bold)

    # This is the template of the xlsx file
worksheet2.write('A2', 'Scan Creation Date:', underline)
worksheet2.write('A3', 'Total Hosts Scanned:')
worksheet2.write('A5', 'OVERALL FIGURES', underline)
worksheet2.write('A6', 'Total Sev 5:')
worksheet2.write('A7', 'Total Sev 4:')
worksheet2.write('A8', 'Total Sev 3:')
worksheet2.write('A9', 'Total Sev 2:')
worksheet2.write('A10', 'Total Sev 1:')
worksheet2.write('A11', 'Overall Total:', underline)
worksheet2.write('A13', 'Hostname')
worksheet2.write('B13', 'IP Address')
worksheet2.write('C13', 'Sev 5')
worksheet2.write('D13', 'Sev 4')
worksheet2.write('E13', 'Sev 3')
worksheet2.write('F13', 'Sev 2')
worksheet2.write('G13', 'Sev 1')
worksheet2.write('H13', 'Total')
worksheet2.write('I13', 'Stats (%)')

所以基本上我可以遍历每一个并指定我需要具有特殊格式。

另外我忘了提到我尝试使用zip来将它们列在一起,然后尝试添加我需要的每个元素,然后我看到它作为一个'元组'所以我无法追加到那些人身上。

1 个答案:

答案 0 :(得分:0)

实现此目的的一种方法是创建一个DataFrame,其中包含您希望在不同列中使用的所有不同类型的格式。然后,您可以使用pandas.DataFrame.iterrows()循环遍历特定列。

我在下面提供了一个示例,该示例使用一个用户定义的函数,该函数的参数基于您要从数据框中使用的格式列。

import xlsxwriter
import pandas as pd

df = pd.DataFrame({
"cell": ['A2', 'A3', 'A5', 'A6', 'A7'], 
"name": ['Scan Creation Date:', 'Total Hosts Scanned:', 
'OVERALL FIGURES', 'Total Sev 5:', 'Total Sev 5:'],
"format1": ['bold', 'none', 'bold', 'none', 'bold'],
"format2": ['underline', 'none', 'underline', 'none', 'none'],
"format3": ['underline', 'bold', 'underline', 'none', 'bold']
})

workbook   = xlsxwriter.Workbook('test.xlsx')

none = workbook.add_format({})
bold = workbook.add_format({'bold': True,})
underline = workbook.add_format({'underline': True,})

def make_sheets(format_col):
    ws = workbook.add_worksheet()
    for i, row in df.iterrows():
        if df[''+ format_col +''].ix[i] == 'bold':
            format = bold
        elif df[''+ format_col +''].ix[i] == 'underline':
            format = underline
        else:
            format = none
        ws.write('%s' % (df['cell'].ix[i]), '%s' % (df['name'].ix[i]), 
format)

make_sheets("format1")
make_sheets("format2")
make_sheets("format3")

workbook.close()

输出:

Expected Output