Django没有名为win32com的模块?

2024-09-27 23:21:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个问题。我必须从Mac电脑(OSX)部署我的Django项目。但我得到了这个错误: 没有名为win32com的模块 有没有一种方法或替代图书馆

这就是我需要它的地方: 视图.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()

基本上,我需要它自动适应(更大的)细胞


Tags: columnstrueapplicationmac部署excelexsheet
1条回答
网友
1楼 · 发布于 2024-09-27 23:21:58

查看此库openpyxl

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

阅读手册的简单用法:

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

在您的情况下,应该使用openpyxl.utils.get_column_letter自动拟合单元格

你可以看看下面的答案

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

相关问题 更多 >

    热门问题