Operation python-excel

Xlsxwriter application modules into the test data is plotted and export to excel

1. Quote:

import xlsxwriter

2. Get workbook:

workbook = xlsxwriter.Workbook(excel_path)

3. Create a worksheet:

worksheet1 = workbook.add_worksheet("sheet1")

4. copy ingress:

= Headings [ ' A ' , ' B ' , ' C ' ] 
worksheet1.write_row ( ' A1 ' , Headings) starting from cell A1 #

The write column:

= DATAl [ ' . 1 ' , ' 2 ' , ' . 3 ' ] 
worksheet1.write_column ( ' A2 ' , DATAl) starting from cell A2 #

6. Create a line chart:

chart = workbook.add_chart({'type': 'line'})

7. designating data is line:

= values ' = $ A $ the Request. 1:! $ A $ ' + STR (len (data1)) # designating data area A1 starting from here to the length of data1 
chart.add_series ({ ' name ' : ' value ' , ' values ' : values}) # Specifies the name and the data of the polyline

8. FIG polyline specified name:

chart.set_title({'name': 'Request Sequence'})

9. Draw line chart:

worksheet1.insert_chart ( 'C . 1 ' , Chart) # start to draw from the cell C1

10. Close the workbook:

workbook.close()

11. Scatter:

    chart = workbook.add_chart({'type': 'scatter'})

    # Set data scope
    data_length = len(data1) + 1
    values = '=sheet1!$B$2:$B$' + str(data_length)
    categories = '=sheet1!$A$2:$A$' + str(data_length)

    # Add series
    chart.add_series({'name': 'data1', 'values': values, 'categories': categories})

    # Set chart title and axis labels
    chart.set_title({'name': 'scatter_chart'})
    chart.set_x_axis({'name': 'x'})
    chart.set_y_axis({'name': 'y'})

    # Insert chart
    worksheet1.insert_chart('G1', chart, {'x_scale': 2, 'y_scale':2}) #x_scale y_scale for scaling graphics and, here, two times

12. Use of win32com.client FIG derived Dispatch:

from win32com.client Import the Dispatch 

DEF export_chart (Workbook, Worksheet, image_path): 
    image_type = ' JPG ' 

    Excel = the Dispatch ( " the Excel.Application " ) 
    excel.Visible = True 

    WB = excel.Workbooks.Open (Workbook) 
    Sheet = WB. Worksheets (Worksheet) 

    for Chart in sheet.ChartObjects (): 
        chart.Chart.Export (image_path, image_type) 

    wb.Close (False) # If there is an error above, excel will remain open, do not know how to solve. 
    excel.Quit ()

 

Guess you like

Origin www.cnblogs.com/workingdiary/p/11351867.html