2

I have a sample excel doc. Two sheets of this doc contain a long description of the doc, I am writing a script to generate some dynamic excel data , I will need add these two static page into my result. What is the best way? I can only think of creating a long data list, then do a add_table. But the text is quite large and big, wonder if there is any better way.

Kaiyu Li
  • 121
  • 3
  • 4
  • See [this](http://stackoverflow.com/questions/18002133/xlsxwriter-is-there-a-way-to-open-an-existing-worksheet-in-my-workbook) link on stackoverflow. – Kevin Zhu Oct 13 '16 at 06:45

1 Answers1

0

If the long descriptions are static then you could store them in text files and add them to each workbook like this:

import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx')

worksheet = workbook.add_worksheet('Desc 1')
textfile = open('description_1.txt')

# Write the first description worksheet.
row = col = 0
for line in textfile:
    worksheet.write(row, col, line.rstrip("\n"))
    row += 1

worksheet = workbook.add_worksheet('Desc 2')
textfile = open('description_2.txt')

# Write the second description worksheet.
row = col = 0
for line in textfile:
    worksheet.write(row, col, line.rstrip("\n"))
    row += 1

# Now add a new worksheet and add new data.
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello')

workbook.close()

The repeated code could be put into a function. If you are reading non ASCII text then use the correct encoding. See the examples in the docs.

jmcnamara
  • 38,196
  • 6
  • 90
  • 108