While creating an Excel file from a Pandas DataFrame is super easy, you cannot easily customize it. In this recipe, you will learn how to use the XlsxWriter
Python library to create a highly customizable report in Excel.
First, import the Python libraries that you need:
from pymongo import MongoClient import pandas as pd from time import strftime
Next, create a connection to MongoDB, and specify the accidents collection:
client = MongoClient('localhost', 27017) db = client.pythonbicookbook collection = db.accidents
Once you have created the connection, run a query to retrieve the first
1000
records in which an accident happened on a Friday:data = collection.find({"Day_of_Week": 6}).limit(1000)
Next, define the name of the report and where it should be saved to, and set up the Excel workbook:
report_file_name = "accident_data_report_{}.xlsx".format(strftime("%m_%d_%y")) report_file = PATH_TO_FILE + "/" + report_file_name accident_report...