How to Generate Report Using xlswriter Module in Python?

How to Generate Report Using xlswriter Module in Python?
  • Generate basic excel report-


import xlsxwriter

# Create a workbook and add a worksheet.

workbook = xlsxwriter.Workbook(‘ExcelTutorial.xlsx’)

worksheet = workbook.add_worksheet()

# Some random data we want to insert into our excel

expenses = (

[‘A’, 1000],

[‘B’,   100],

[‘C’,  300],

[‘D’,    50],



# Start from the first cell. Rows and columns are zero indexed.

row = 0

col = 0

# Iterate over the data and write it out row by row.

for item, cost in (expenses):

worksheet.write(row, col,     item)

worksheet.write(row, col + 1, cost)

row += 1

# Write a total using a formula.

worksheet.write(row, 0, ‘Total’)

worksheet.write(row, 1, ‘=SUM(B1:B4)’)

# Close the excel file using close()



2)Merging cells-

import xlsxwriter

# Create an new Excel file and add a worksheet.

workbook = xlsxwriter.Workbook(‘merge_cells.xlsx’)

worksheet = workbook.add_worksheet()


# Increase the cell size of the merged cells to highlight the formatting.

worksheet.set_column(‘B:D’, 12)

worksheet.set_row(3, 30)

worksheet.set_row(6, 30)

worksheet.set_row(7, 30)


# Create a format to use in the merged range.

merge_format = workbook.add_format({

‘bold’: 1,

‘border’: 1,

‘align’: ‘center’,

‘valign’: ‘vcenter’,

‘fg_color’: ‘red’})


# Merge 3 cells.

worksheet.merge_range(‘B4:D4’, ‘Merged Range’, merge_format)


# Merge 3 cells over two rows.

worksheet.merge_range(‘B7:D8’, ‘Merged Range’, merge_format)


For any further queries feel free to Contact Us we would be glad to help you.