Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report