Writing to an Excel spreadsheet

后端 未结 12 1865
不知归路
不知归路 2020-11-22 04:39

I am new to Python. I need to write some data from my program to a spreadsheet. I\'ve searched online and there seem to be many packages available (xlwt, XlsXcessive, openpy

12条回答
  •  感情败类
    2020-11-22 05:27

    You can try hfexcel Human Friendly object-oriented python library based on XlsxWriter:

    from hfexcel import HFExcel
    
    hf_workbook = HFExcel.hf_workbook('example.xlsx', set_default_styles=False)
    
    hf_workbook.add_style(
        "headline", 
        {
           "bold": 1,
            "font_size": 14,
            "font": "Arial",
            "align": "center"
        }
    )
    
    sheet1 = hf_workbook.add_sheet("sheet1", name="Example Sheet 1")
    
    column1, _ = sheet1.add_column('headline', name='Column 1', width=2)
    column1.add_row(data='Column 1 Row 1')
    column1.add_row(data='Column 1 Row 2')
    
    column2, _ = sheet1.add_column(name='Column 2')
    column2.add_row(data='Column 2 Row 1')
    column2.add_row(data='Column 2 Row 2')
    
    
    column3, _ = sheet1.add_column(name='Column 3')
    column3.add_row(data='Column 3 Row 1')
    column3.add_row(data='Column 3 Row 2')
    
    # In order to get a row with coordinates:
    # sheet[column_index][row_index] => row
    print(sheet1[1][1].data)
    assert(sheet1[1][1].data == 'Column 2 Row 2')
    
    hf_workbook.save()
    

提交回复
热议问题