6

I've read a lot of stackoverflow and other threads where it's been mentioned how to read excel binary file.

Reference: Read XLSB File in Pandas Python

import pandas as pd
df = pd.read_excel('path_to_file.xlsb', engine='pyxlsb')

However, I can not find any solution on how to write it back as .xlsb file after processing using pandas? Can anyone please suggest a workable solution for this using python?

Any help is much appreciated!

2
  • Does this answer your question? How do I convert a csv file to xlsb using Python? Commented Aug 25, 2020 at 8:18
  • 2
    @Let'stry: I'll say probably not(there are minor error reported for above solution). Is there any other more concise and or cleaner way to do it (similar to how I am reading it). Actually, in my case, I am trying to write processed pandas dataframe as an .xlsb output without losing any functionality(formulas, etc) in .xlsb files. I am being extra cautious as these files will go to production. Commented Aug 25, 2020 at 13:28

3 Answers 3

1

I haven't been able to find any solution to write into xlsb files or create xlsb files using python.

But maybe one work around is to save your file as xlsx using any of the many available libraries to do that (such as pandas, xlsxwriter, openpyxl) and then converting that file into a xlsb using xlsb-converter. https://github.com/gibz104/xlsb-converter

CAUTION: This repository uses WIN32COM, which is why this script only supports Windows

Sign up to request clarification or add additional context in comments.

Comments

0

Writing xlsb files from python isn't supported in pyxlsb library. Aspose.Cells for Python is capable of building cross-platform applications with the ability to generate, modify, convert, render and print all Excel files inclding xlsb file. It is a commercial tool.

It requires Python <=3.13, >=3.8.

Here's the sample code:

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook, FileFormatType

# Create Workbook object.
workbook = Workbook(FileFormatType.XLSB)

# Access the first worksheet of the workbook.
worksheet = workbook.getWorksheets().get(0)

# Get the desired cell(s) of the worksheet and input the value into the cell(s).
worksheet.getCells().get("A1").putValue("ColumnA")
worksheet.getCells().get("B1").putValue("ColumnB")
worksheet.getCells().get("A2").putValue("ValueA")
worksheet.getCells().get("B2").putValue("ValueB")

# Save the workbook as XLSB file.
workbook.save("output.xlsb")

jpype.shutdownJVM()

Other options are writing .xlsx in Python (pandas, openpyxl or xlsxwriter) and then manually saving as .xlsb from Excel or win32com.client (Windows only)

Comments

-4

you can read binary file with open_workbook under pyxlsb. Please find below the code:

import pandas as pd
from pyxlsb import open_workbook
path=r'D:\path_to_file.xlsb'
df2=[]
with open_workbook(path) as wb:
    with wb.get_sheet(1) as sheet:
       for row in sheet.rows():
           df2.append([item.v for item in row])
data= pd.DataFrame(df2[1:], columns=df2[0])

1 Comment

The question is for writing into xlsb files not reading from it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.