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. I want to export this data to Special Excel file and Special Sheet. But not just export, I want add row = 0 with Headers of table. For example: header = [('Place', 'Players', 'Score', 'Delta', 'Game')]. For each row from SQLite I need add index to Place column from 1 to XXX. Headers should be simple configure in future.
I try to directly import data from sqlite to excel, but in this case header not added. (here Players_Last_Day_Stat - sql select) 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)
Also, hot to change some cell bolt from python?
Thank you.
CodePudding user response:
You're close. To make an index for your table, you can use 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)