Get the App
SLTechnology News&Howtos  ›  Database  › 

How python generates EXCEL, connects to a database, and writes specified data to EXCEL

Shulou Source: shulou.com Published: 2022-05-31 13:55:47 09月26日 Update

Editor to share with you how python generates EXCEL, connects to the database, and writes the specified data into EXCEL. I hope you will get something after reading this article. Let's discuss it together.

#! / usr/bin/python

#-*-coding: utf-8-*-

#! / usr/bin/env python

Import MySQLdb

Import sys

Import xlsxwriter

Reload (sys)

Sys.setdefaultencoding ('utf-8')

# create an EXCEL file

Workbook = file name of xlsxwriter.Workbook ('name.xlsx') # EXCEL

# create a worksheet object

Worksheet_1 = workbook.add_worksheet ('name_1') # name of sheet page 1

Worksheet_2 = workbook.add_worksheet ('name_2') # name of sheet page 2

# need more sheet pages, and so on

Title_1 = # define the list of data headers

Title_2 = [upright xxxx recording, upright xxxx']

Format_1 = workbook.add_format () # define format format object

Format_1.set_border (1) # defines the format for format object cell borders to be thickened (1 pixel)

Format_title_1 = workbook.add_format () # define format_title format object

Format_title_1.set_border (1) # defines a formatting object with a format_title object cell border thickened (1 pixel)

Format_title_1.set_bg_color ('# cccccc') # defines the format in which the background color of the format_title object cell is'# cccccc'

Format_title_1.set_align ('center') # defines the format for the center alignment of format_title object cells

Format_title_1.set_bold () # defines the bold format of the cell content of the format_title object

Worksheet_1.write_row ('A1 formatting title1)

Conn = MySQLdb.connect (host= "xxx", user= "xxx", passwd= "xxx", db= "xxx", port=3306,charset='utf8') # Connect to the database, host: database link, user: account, passwd: password, db: database name

Cursor_1 = conn.cursor ()

Cursor_1.execute ("select xxx") # execute sql statement

Data_1 = cursor_1.fetchall () # data assignment to data_1

List_1=range (len (data_1)) # measures the length of data_1 and lists the values assigned to list_1 in range

# write the EXCEL table cyclically according to a certain format and location, and you can try the specific location on your own

For i in list_1:

For j in range (4):

Worksheet_1.write (iAccording 1, JJ, data, writing 1 [I] [j]) # write to EXCEL form

I + = 1

Cursor_1.close ()

Conn.close ()

Workbook.close ()

After reading this article, I believe you have a certain understanding of "how python generates EXCEL, connects to the database, and writes specified data into EXCEL". If you want to know more about it, please follow the industry information channel and thank you for your reading!

Tags: Data objects formats databases units pages and will generate location pixels names files more articles tables borders and so on over password file name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi vpn Linux Shulou Technology Shulou Information