0

I got this issue. I have to deploy my Django project from a Mac computer(OSX). But I get this error: No module named win32com Is there a way or alternative library?

This is where I need it: views.py

excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = True
ex = excel.Workbooks.Open(save_path)
ex_sheet = ex.Worksheets('Finansal Tablolar_formul')
ex_sheet.Columns.AutoFit()
ex_sheet_2 = ex.Worksheets('Finansal Tablolar_formul')
ex_sheet_2.Columns.AutoFit()
ex.Save()
ex.Close(True)
excel.Application.Quit()

Basically I need it for autofit(larger) the cells

GwynBleidD
  • 20,081
  • 5
  • 46
  • 77
edche
  • 636
  • 6
  • 33

1 Answers1

1

Check this out library openpyxl

https://openpyxl.readthedocs.io/en/stable/

Simple usage of reading workbook:

import openpyxl

file_path = "path to your file.xlsx"
wb_obj = openpyxl.load_workbook(filename=file_path, data_only=True)
sheet_obj = wb_obj['your sheet name']
# or just open the active sheet
# sheet_obj = wb_obj.active
max_col = sheet_obj.max_column

for i in range(1, max_row + 1):
    column_1 = sheet_obj.cell(row = i, column = 1).value
    column_2 = sheet_obj.cell(row = i, column = 2).value
    column_3 = sheet_obj.cell(row = i, column = 3).value

In your case, you should use openpyxl.utils.get_column_letter to auto fitting the cells.

You can look at below answer.

https://stackoverflow.com/a/40935194/10515127

MertG
  • 753
  • 1
  • 6
  • 22
  • That isn't going to help with `sheet.Columns.AutoFit()` which was an explicit requirement of the question. – BoarGules Apr 30 '21 at 16:31