O'Reilly logo

Python Business Intelligence Cookbook by Robert Dempsey

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating customizable Excel reports using XlsxWriter

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.

How to do it…

  1. First, import the Python libraries that you need:
    from pymongo import MongoClient
    import pandas as pd
    from time import strftime
  2. Next, create a connection to MongoDB, and specify the accidents collection:
    client = MongoClient('localhost', 27017)
    db = client.pythonbicookbook
    collection = db.accidents
  3. 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) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required