Tuesday, 14 January 2014

Generate a excel or xlsx file using Python?

In previous post I have shown how to create or generate docx file using python. same as we can generate excel or xlsx file using python.

Same steps you have to follow. Here only we have to download XlsxWriterfrom here Download

Now start install this xlsxwriter module in your system, type command on C prompt with xlsxwriter path

C:\user\ashish\Desktop\xlsxwriter> python setup.py install




Keep create_xlsx.py file in xlsxwriter module becoz all library is present in this folder.
now write below code for generating windows xlsx file

create_xlsx.py 
 import xlsxwriter
import os
x=str(os.path.abspath(os.path.join(os.path.expanduser('~'), 'Desktop')))
workbook = xlsxwriter.Workbook(x+'\panes5.xlsx')
worksheet1 = workbook.add_worksheet('Report')
header_format = workbook.add_format({'bold': True,'align': 'center','valign': 'vcenter','fg_color': '#D7E4BC','border': 1})
center_format = workbook.add_format({'align': 'center'})
left_format = workbook.add_format({'align': 'left'})
Thank_format = workbook.add_format({'align': 'left', 'bold': True,'valign': 'vcenter','font_size':13,'border': 0})
Detail_format = workbook.add_format({'align': 'left', 'bold': True,'valign': 'vcenter','font_size':11,'border': 0})
merge_format = workbook.add_format({'bold': True,'border': 0,'align':'center','valign':'vcenter','font_size':18,'font_color':'red',})
worksheet1.freeze_panes(9, 9)
worksheet1.set_column('A:D', 50)
worksheet1.set_row(10, 20)
worksheet1.set_selection('E9')
worksheet1.set_tab_color('red')
worksheet1.set_column(0, 0, 9)
worksheet1.set_column(1, 1, 100)
worksheet1.set_column(2, 2, 25)
worksheet1.set_column(3, 3, 20)
worksheet1.set_column(4, 4, 25)
headr=['Sr.no.','Name', 'Email', 'Mobile', 'city']
worksheet1.merge_range('B1:B2', 'Report', merge_format)
worksheet1.write(3, 1, "Name- Ashish", Detail_format)
worksheet1.write(4, 1, "Email address- example@gmail.com", Detail_format)
worksheet1.write(5, 1, "mobile- 5464449464", Detail_format)
worksheet1.write(6, 1, "city- abc", Detail_format)
# Some text to demonstrate scrolling.
for col in range(0, 5):
    worksheet1.write(8, col, headr[col], header_format)
points = ['C:\user\ashish\Desktop\python-docx\docx.py',
              'C:\user\ashish\Desktop\python-docx\abc.py',
              'welcome to python docx',
              'welcome to my python blog',
              ]        
for row in range(9, len(points)+9):
    worksheet1.write(row, 0, row-8, center_format)
    worksheet1.write(row, 1, points[row-9], left_format)
    worksheet1.write(row, 2, 'example@gmail.com', center_format)
    worksheet1.write(row, 3, '45466464646', center_format)
    worksheet1.write(row, 4, 'abc', center_format)
    last=row
    
worksheet1.write(last+4, 1, "Thanks & Regards", Thank_format)
worksheet1.write(last+5, 1, "ashish")
worksheet1.insert_image('B'+str(last+7), 'image1.png')
workbook.close()

For more information about xlsx writer means more features to make xlsx file then follow this link

Thanks guys

No comments:

Post a Comment