简体   繁体   English

Python Sqlite 和 Excel output 带有标题和行 ID

[英]Python Sqlite and Excel output with headers and Row ID

Please help to find correct solution from "simple to customize in future" point of view.请从“将来易于定制”的角度帮助找到正确的解决方案。 I have SQLite table and very big select. After this select I got 5 column and any rows.我有 SQLite 表和非常大的 select。在这个 select 之后我有 5 列和任何行。 I want to export this data to Special Excel file and Special Sheet.我想将此数据导出到 Special Excel 文件和 Special Sheet。 But not just export, I want add row = 0 with Headers of table.但不仅仅是导出,我想添加 row = 0 和表头。 For example: header = [('Place', 'Players', 'Score', 'Delta', 'Game')].例如:header = [('Place', 'Players', 'Score', 'Delta', 'Game')]。 For each row from SQLite I need add index to Place column from 1 to XXX.对于 SQLite 中的每一行,我需要将索引添加到从 1 到 XXX 的 Place 列。 Headers should be simple configure in future.标题应该在未来简单配置。

I try to directly import data from sqlite to excel, but in this case header not added.我尝试直接从sqlite导入数据到excel,但在这种情况下没有添加header。 (here Players_Last_Day_Stat - sql select) from xlsxwriter.workbook import Workbook workbook = Workbook('Total_Stat.xlsx') (此处选择 Players_Last_Day_Stat - sql) from xlsxwriter.workbook import Workbook workbook = Workbook('Total_Stat.xlsx')

conn = create_connection()
c=conn.cursor()

worksheet = workbook.add_worksheet('Last-Day')
mysel=c.execute(Players_Last_Day_Stat)
for i, row in enumerate(mysel):
    for j, value in enumerate(row):
        if isinstance(value, float):
            value = int(value)
        worksheet.write(i, j, value)

But result like this但是这样的结果在此处输入图像描述

I expect this result finally:我最终期待这个结果: 在此处输入图像描述

Also, hot to change some cell bolt from python?另外,很想从 python 更换一些电池螺栓吗?

Thank you.谢谢你。

You're close.你很接近。 To make an index for your table, you can use worksheet.write_column .要为您的表创建索引,您可以使用worksheet.write_column Here is what you can do to implement that (based on your code) and to shift the table ( one column to the right and one row below ):这是您可以执行的操作(根据您的代码)并移动表格(一列向右,一行在下方):

from xlsxwriter.workbook import Workbook

workbook = Workbook('Total_Stat.xlsx')
worksheet = workbook.add_worksheet('Last-Day')

conn = create_connection()
c = conn.cursor()
mysel = c.execute(Players_Last_Day_Stat)

header = ['Place', 'Players', 'Score', 'Delta', 'Game']
for idx, col in enumerate(header):
    worksheet.write(0, idx, col) # <- write the column name one time in a row

#this was untouched
for i, row in enumerate(mysel):
    for j, value in enumerate(row):
        if isinstance(value, float):
            value = int(value)
        worksheet.write(i+1, j+1, value)

worksheet.write_column(1, 0, [i for i in range(1, len(c.execute(Players_Total_Stat).fetchall()) + 1)]) # make an index

#here, we make both 1st column/row bold
bold_fmt = workbook.add_format({'bold': True})
worksheet.set_row(0, None, bold_fmt)
worksheet.set_column(0, 0, None, bold_fmt)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM