How to use 'export dataframe to excel python' in Python

Every line of 'export dataframe to excel python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
634def export_matrix_to_excel(row_names, col_names, matrix, filepath='export.xlsx', sheetname='Export'):
635 workbook = xlsxwriter.Workbook(filepath)
636 ws = workbook.add_worksheet(sheetname)
637 # formatting
638 # border
639 format_border = workbook.add_format()
640 format_border.set_border(1)
641 format_border.set_font_size(9)
642 # border + text wrap
643 format_border_text_wrap = workbook.add_format()
644 format_border_text_wrap.set_text_wrap()
645 format_border_text_wrap.set_border(1)
646 format_border_text_wrap.set_font_size(9)
647 # set column width
648 ws.set_column(0, 1, width=15, cell_format=None)
649 ws.set_column(1, 50, width=9, cell_format=None)
650 # write data
651 for i, p in enumerate(col_names): # process names
652 ws.write(0, i+1, p, format_border_text_wrap)
653 for i, p in enumerate(row_names): # product names
654 ws.write(i+1, 0, p, format_border)
655 for i, row in enumerate(range(matrix.shape[0])): # matrix
656 ws.write_row(i+1, 1, matrix[i, :], format_border)
657 workbook.close()
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

49def write(self, row_n, col_n, value):
50 '''写入数据,如(2,3,"hello"),第二行第三列写入数据"hello"'''
51 self.ws.cell(row_n, col_n).value = value
52 self.wb.save(self.filename)

Related snippets