如何从sql数据库中导出具有多个工作表的xlsx文件并使用python

时间:2019-06-19 11:25:14

标签: python pandas sqlalchemy xlsxwriter

我想通过使用查询从sql数据库中导出具有多个工作表的xlsx文件 现在,我已经创建了三个查询,每个查询对某些特定数据都有一个SELECT顺序,现在我需要用一个xlsx文件从sql数据库中导出,但包含三张表。我新如何从sql数据库中将数据导出为xlsx文件,但是只用一张纸,这是我的代码

from sqlalchemy import create_engine
import pandas as pd
import os
import csv
import MySQLdb
from sqlalchemy import types, create_engine
from xlsxwriter.workbook import Workbook


# MySQL Connection
MYSQL_USER      = 'root'
MYSQL_PASSWORD  = 'xxxxxxxxxx'
MYSQL_HOST_IP   = '127.0.0.1'
MYSQL_PORT      = 3306
MYSQL_DATABASE  = 'mydb'
govtracker_table = 'govtracker' # table you want to save

con = MySQLdb.connect(user=MYSQL_USER, passwd=MYSQL_PASSWORD, 
host=MYSQL_HOST_IP, db=MYSQL_DATABASE)
cursor = con.cursor()

export_frm_db_query_ssv = "SELECT id, site_code, site_name, region, 
site_type, tac_name, dt_readiness, rfs, " \
    "rfs_date, acceptance_date_opt, acceptance_date_plan, signed_sites, " 
\
    "as_built_date, as_built_status, cluster_name, 
type_standalone_colocated, " \
    "installed_type_standalone_colocated, status, pending, pending_status, 
" \
                      "problematic_details, ets_tac, region_r, 
sf6_signed_date, " \
                      "sf6_signed_comment, comment_history, on_air_owner, 
pp_owner, " \
                      "report_comment, hu_opt_area_owner, planning_owner, 
po_number, " \
                      "trigger_date, as_built_status_tr FROM %s;" % 
govtracker_table

workbook = Workbook('SSV Progress Tracker_23 May.xlsx')
sheet = workbook.add_worksheet()
for r, row in enumerate(cursor.fetchall()):
for c, col in enumerate(row):
    sheet.write(r, c, col)
workbook.close()

以前的代码只是将数据库中的数据提取到一张纸中的xlsx文件中

现在我已经创建了三个查询,每个查询应位于三个不同的工作表中

#SHR Phase 1 sheet SQL query
export_frm_db_query_shr_ph1 = "SELECT id, site_code, site_name, region, 
site_type, tac_name, dt_readiness, rfs, " \
    "rfs_date, huawei_1st_submission_date, te_1st_submission_date, " \
                          "huawei_2nd_submission_date, 
te_2nd_submission_date, " \
                          "huawei_3rd_submission_date, 
te_3rd_submission_date, " \
                          "acceptance_date_opt, acceptance_date_plan, 
signed_sites, " \
    "as_built_date, as_built_status, date_dt, dt_status, " \
    "shr_status, dt_planned, integeration_status, comments_snags WHERE 
site_type = 'Existing' AND " \
                          "site_type = 'New' AND region = 'Cairo' AND 
region = 'Red sea & Sinai' " \
                          "FROM %s;" % govtracker_table

#SHR Phase 2 sheet SQL query
export_frm_db_query_shr_ph2 = "SELECT id, site_code, site_name, region, 
site_type, tac_name, dt_readiness, rfs, " \
    "rfs_date, huawei_1st_submission_date, te_1st_submission_date, " \
                          "huawei_2nd_submission_date, 
te_2nd_submission_date, " \
                          "huawei_3rd_submission_date, 
te_3rd_submission_date, " \
                          "acceptance_date_opt, acceptance_date_plan, 
signed_sites, " \
    "as_built_date, as_built_status, date_dt, dt_status, " \
    "shr_status, dt_planned, integeration_status, comments_snags WHERE 
site_type = 'Existing' AND " \
                          "site_type = 'New' AND region = 'Delta' " \
                          "FROM %s;" % govtracker_table

#SHR Phase 3 sheet SQL query
export_frm_db_query_shr_ph3 = "SELECT id, site_code, site_name, region, 
site_type, tac_name, dt_readiness, rfs, " \
    "rfs_date, huawei_1st_submission_date, te_1st_submission_date, " \
                          "huawei_2nd_submission_date, 
te_2nd_submission_date, " \
                          "huawei_3rd_submission_date, 
te_3rd_submission_date, " \
                          "acceptance_date_opt, acceptance_date_plan, 
signed_sites, " \
    "as_built_date, as_built_status, date_dt, dt_status, " \
    "shr_status, dt_planned, integeration_status, comments_snags WHERE 
site_type = 'Cow' " \
                          "site_type = 'Indoor' " \
                          "FROM %s;" % govtracker_table

现在我需要在一个xlsx文件中的三个工作表中通过这三个查询从mydatabase导出,这是工作表中的每个查询

最后,现在我想将列放在导出的xlsx文件中,但是要更改其名称,并仅向该列添加颜色,并更改文本样式以及如何重命名工作表名称而不是默认名称

我希望这足够清楚

1 个答案:

答案 0 :(得分:1)

您可以使用pandas xlsxwriter,如下所示:

writer1 = pd.ExcelWriter('xlsx file name', engine='xlsxwriter')
your_dataframe1['column based conditions'].to_excel(writer1, sheet_name='name of sheet 1', index=False, startrow=0)
your_dataframe2['column based conditions'].to_excel(writer1, sheet_name='name of sheet 2', index=False, startrow=0)
your_dataframe3.to_excel(writer1, sheet_name='name of sheet 3', index=False, startrow=0) #no conditions here

只需确保您的数据集是数据帧类型。当您的Excel就绪时,请输入:

writer1.save()

保存。

如果要格式化数据框标题,则应导入:

import pandas.io.formats.excel

然后输入:

pandas.io.formats.excel.header_style

要格式化excel文件,您可以浏览openpyxl模块(我没有现成的解决方案),也可以导入win32com.client并使用VBA宏格式化表格(这是非常简单有效的方法)。

请记住,在以这种方式进行编辑之前,您需要保存作家(writer1.save())

import win32com.client

workbook1 = xl.Workbooks.Open(Filename='file path' + 'file_name.xlsx', ReadOnly=1)
xl.Application.Run('PERSONAL.XLSB!personal macro name')
workbook1.Close(True)

要轻松地重命名列,您可以使用以下代码:

dataframe = dataframe.rename(index=str, columns={'ID' : 'col2','CATEGORY' : 'col1','DATE' : 'col3'})

dataframe = dataframe[['col1','col2','col3']]