0

I want to create a fixed format for ExcelWriter in Pandas which I could use in many scripts, without needing to write it everytime. I know I can create a format with:

import pandas as pd
writer = pd.ExcelWriter('demo.xlsx', engine='xlsxwriter')
workbook = writer.book
num_fmt = workbook.add_format({'num_format': '#,##0.0'})

And then use num_fmt with:

worksheet.set_column('H:H', 8, num_fmt)

But I'm looking for a way to avoid re-defining num_fmt in every script I write. I have a self-defined package named my_pack that I import to every script, and I want to somehow define num_fmt there, so I could use it like:

worksheet.set_column('H:H', 8, my_pack.num_fmt)

without defining num_fmt in the current script. Is that possible?

Aryerez
  • 3,417
  • 2
  • 9
  • 17
  • https://stackoverflow.com/questions/18002133/xlsxwriter-is-there-a-way-to-open-an-existing-worksheet-in-my-workbook – challa420 Jun 02 '20 at 11:42

1 Answers1

1

I have a self-defined package named my_pack that I import to every script, and I want to somehow define num_fmt there ... without defining num_fmt in the current script. Is that possible?

No. Not with XlsxWriter.

You need to instantiate a format from a Workbook object and it is unique to each workbook, so you won't be able to create it once, store it, and then reuse it in different workbooks.

jmcnamara
  • 38,196
  • 6
  • 90
  • 108