0

Hello I have data in list of dicts. I'm using panda DataFrame to parse from dict to excel. Data are fetch 5 different places. I would like to add data into different spreadsheet.

Here is the code I'm trying, but it doesn't add new sheets, it overwrites

def write_xlsx(filename, sheetname, data):

workbook = xlsxwriter.Workbook(filename)
checksheet = workbook.get_worksheet_by_name(sheetname)

if checksheet is None:
    worksheet = workbook.add_worksheet(sheetname)

# Store the worksheet objects in a dict indexed by name.
my_worksheets = {}
for worksheet in workbook.worksheets():
    my_worksheets[worksheet.get_name()] = worksheet

df = pd.DataFrame(data)
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter(filename, engine='xlsxwriter')

df.to_excel(writer, sheet_name=sheetname, index=False, startrow=1, header=False)
# Set the column widths
workbook  = writer.book
worksheet = writer.sheets[sheetname]

header_format = workbook.add_format({'text_wrap': True})
columns_format = workbook.add_format({'text_wrap': True})

# Write the column headers with the defined format.
for col_num, value in enumerate(df.columns.values):
    worksheet.write(0, col_num + 0, value, header_format)

writer.save()
miu
  • 189
  • 2
  • 13
  • Would [this answer help? https://stackoverflow.com/a/20221655/12534623 Alternatively you could create and write to all sheets under one function call as described here: https://stackoverflow.com/a/46599445/12534623 – inverzeio May 20 '20 at 23:10
  • Thanks, I was looking at, but couldn't figure the the header formatting and column, as I needed to wrap the text from the given column. – miu May 20 '20 at 23:15
  • How to format the header and column to wrap the text? – miu May 21 '20 at 00:48
  • can you give an example? A screenshot would help on the expected output, as well as some sample data to run your code with – inverzeio May 21 '20 at 19:28

0 Answers0