How do you write in an excel file using python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    XlsxWriter is a Python module for writing files in the XLSX file format. It can be used to write text, numbers, and formulas to multiple worksheets. Also, it supports features such as formatting, images, charts, page setup, auto filters, conditional formatting and many others.
    Use this command to install xlsxwriter module: 
     

     pip install xlsxwriter 

      
    Note: Throughout XlsxWriter, rows and columns are zero indexed. The first cell in a worksheet, A1 is (0, 0), B1 is (0, 1), A2 is (1, 0), B2 is (1, 1) ..similarly for all.
    Let’s see how to create and write to an excel-sheet using Python.
    Code #1 : Using A1 notation(cell name) for writing data in the specific cells.
     

    Python3

    import xlsxwriter

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

    worksheet = workbook.add_worksheet()

    worksheet.write('A1', 'Hello..')

    worksheet.write('B1', 'Geeks')

    worksheet.write('C1', 'For')

    worksheet.write('D1', 'Geeks')

    workbook.close()

    Output: 
     

    How do you write in an excel file using python?

      
    Code #2 : Using the row-column notation(indexing value) for writing data in the specific cells.
     

    Python3

    import xlsxwriter

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

    worksheet = workbook.add_worksheet()

    row = 0

    column = 0

    content = ["ankit", "rahul", "priya", "harshita",

                        "sumit", "neeraj", "shivam"]

    for item in content :

        worksheet.write(row, column, item)

        row += 1

    workbook.close()

    Output: 
     

    How do you write in an excel file using python?

      
    Code #3 : Creating a new sheet with the specific name 
     

    Python3

    import xlsxwriter

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

    worksheet = workbook.add_worksheet("My sheet")

    scores = (

        ['ankit', 1000],

        ['rahul',   100],

        ['priya'300],

        ['harshita',    50],

    )

    row = 0

    col = 0

    for name, score in (scores):

        worksheet.write(row, col, name)

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

        row += 1

    workbook.close()

    Output: 
     

    How do you write in an excel file using python?

      
    XlsxWriter has some advantages and disadvantages over the alternative Python modules for writing Excel files.
    Advantages: 
     

    • It supports more Excel features than any of the alternative modules.
    • It has a high degree of fidelity with files produced by Excel. In most cases the files produced are 100% equivalent to files produced by Excel.
    • It has extensive documentation, example files and tests.
    • It is fast and can be configured to use very little memory even for very large output files.

    Disadvantages: 
     

    • It cannot read or modify existing Excel XLSX files.

    How do you write to an Excel file in Python?

    # import xlsxwriter module..
    import xlsxwriter..
    book = xlsxwriter.Book('Example2.xlsx').
    sheet = book.add_sheet().
    # Rows and columns are zero indexed..
    row = 0..
    column = 0..
    content = ["Parker", "Smith", "John"].

    How do I write data into an XLSX file in Python?

    Create Excel XLSX Files in Python.
    Create a new object of Workbook class..
    Access the desired Worksheet in the workbook using Workbook. getWorksheets(). get(index) method..
    Put value in the desired cell using Worksheet. getCells(). get(“A1”). ... .
    Save the workbook as . xlsx file using Workbook. save() method..

    How do you read and write in Excel using Python?

    Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files.

    How do I write data in a specific column in Excel using Python?

    So for the cell = 'C%d' % (index + 2) it resolves to the cell reference - that is when index = 0 then cell = 'C2' and I don't think you should change that. You could change ws[cell] = row[0] to ws[cell] = row['Col_C'] and it would work. Hopefully this answers your question.