当前位置: 首页>>代码示例>>Python>>正文


Python excel.save_virtual_workbook方法代码示例

本文整理汇总了Python中openpyxl.writer.excel.save_virtual_workbook方法的典型用法代码示例。如果您正苦于以下问题:Python excel.save_virtual_workbook方法的具体用法?Python excel.save_virtual_workbook怎么用?Python excel.save_virtual_workbook使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在openpyxl.writer.excel的用法示例。


在下文中一共展示了excel.save_virtual_workbook方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: xls_as_xlsx

# 需要导入模块: from openpyxl.writer import excel [as 别名]
# 或者: from openpyxl.writer.excel import save_virtual_workbook [as 别名]
def xls_as_xlsx(xls_file):
    # first open using xlrd
    source_workbook = xlrd.open_workbook(file_contents=xls_file.read())

    # Create the destination workbook, deleting and auto-generated worksheets.
    destination_workbook = openpyxl.Workbook() # TODO: Would like to figure out how to make appends work with a "write_only" workbook.
    for wksht_nm in destination_workbook.get_sheet_names():
        worksheet= destination_workbook.get_sheet_by_name(wksht_nm)
        destination_workbook.remove_sheet(worksheet)

    worksheet_names= ['survey', 'choices']
    for wksht_nm in source_workbook.sheet_names():
        source_worksheet= source_workbook.sheet_by_name(wksht_nm)
        destination_worksheet= destination_workbook.create_sheet(title=wksht_nm)

        for row in xrange(source_worksheet.nrows):
            destination_worksheet.append( [source_worksheet.cell_value(row, col) for col in xrange(source_worksheet.ncols)] )

    return io.BytesIO(save_virtual_workbook(destination_workbook)) 
开发者ID:awemulya,项目名称:kobo-predict,代码行数:21,代码来源:analyser_export.py

示例2: content

# 需要导入模块: from openpyxl.writer import excel [as 别名]
# 或者: from openpyxl.writer.excel import save_virtual_workbook [as 别名]
def content(self, value):
        workbook = None
        if not bool(value) or not len(value):  # Short-circuit to protect against empty querysets/empty lists/None, etc
            self._container = []
            return
        elif isinstance(value, list):
            workbook = self._serialize_list(value)
        elif isinstance(value, QuerySet):
            workbook = self._serialize_queryset(value)
        if django.VERSION < (1, 9):
            if isinstance(value, ValuesQuerySet):
                workbook = self._serialize_values_queryset(value)
        if workbook is None:
            raise ValueError('ExcelResponse accepts the following data types: list, dict, QuerySet, ValuesQuerySet')

        if self.force_csv:
            self['Content-Type'] = 'text/csv; charset=utf8'
            self['Content-Disposition'] = 'attachment;filename="{}.csv"'.format(self.output_filename)
            workbook.seek(0)
            workbook = self.make_bytes(workbook.getvalue())
        else:
            self['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            self['Content-Disposition'] = 'attachment; filename="{}.xlsx"'.format(self.output_filename)
            workbook = save_virtual_workbook(workbook)
        self._container = [self.make_bytes(workbook)] 
开发者ID:tarkatronic,项目名称:django-excel-response,代码行数:27,代码来源:response.py

示例3: _query_results_to_xlsx

# 需要导入模块: from openpyxl.writer import excel [as 别名]
# 或者: from openpyxl.writer.excel import save_virtual_workbook [as 别名]
def _query_results_to_xlsx(query_results: DocumentQueryResults) -> HttpResponse:
    from openpyxl import Workbook
    from openpyxl.writer.excel import save_virtual_workbook
    from openpyxl.styles import Font, Alignment

    wb = Workbook()
    ws = wb.active
    ws.append(query_results.column_titles)
    for cells in ws.rows:
        for cell in cells:
            cell.font = Font(name=cell.font.name, bold=True)
            cell.alignment = Alignment(horizontal='center')
        break

    for row in query_results.fetch():
        ws.append(row)

    def str_len(value):
        return len(str(value)) if value is not None else 0

    for column_cells in ws.columns:
        length = min(max(str_len(cell.value) for cell in column_cells), 100) + 1
        ws.column_dimensions[column_cells[0].column_letter].width = length

    response = HttpResponse(save_virtual_workbook(wb),
                            content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    response['Content-Disposition'] = 'attachment; filename=export.xlsx'
    return response 
开发者ID:LexPredict,项目名称:lexpredict-contraxsuite,代码行数:30,代码来源:v1.py

示例4: save_virtual_workbook

# 需要导入模块: from openpyxl.writer import excel [as 别名]
# 或者: from openpyxl.writer.excel import save_virtual_workbook [as 别名]
def save_virtual_workbook(self):
        self.sort_worksheets()
        return save_virtual_workbook(self.workbook) 
开发者ID:SverkerSbrg,项目名称:openpyxl-templates,代码行数:5,代码来源:templated_workbook.py

示例5: createResponse

# 需要导入模块: from openpyxl.writer import excel [as 别名]
# 或者: from openpyxl.writer.excel import save_virtual_workbook [as 别名]
def createResponse(self):
        response = Response(content_type='application/vnd.ms-excel')
        response.body = save_virtual_workbook(self.w)
        return response 
开发者ID:muesli-hd,项目名称:muesli,代码行数:6,代码来源:viewsGrading.py

示例6: export_xlsx

# 需要导入模块: from openpyxl.writer import excel [as 别名]
# 或者: from openpyxl.writer.excel import save_virtual_workbook [as 别名]
def export_xlsx(modeladmin, request, queryset):
    wb = Workbook()
    ws = wb.active
    ws.title = "Products"

    number_alignment = Alignment(horizontal="right")
    wb.add_named_style(
        NamedStyle(
            "Identifier", alignment=number_alignment, number_format=FORMAT_NUMBER
        )
    )
    wb.add_named_style(
        NamedStyle("Normal Wrapped", alignment=Alignment(wrap_text=True))
    )

    column_config = {
        "A": ColumnConfig("ID", width=10, style="Identifier"),
        "B": ColumnConfig("Title", width=30),
        "C": ColumnConfig("Description", width=60),
        "D": ColumnConfig("Price", width=15, style="Currency", number_format="#,##0.00 €"),
        "E": ColumnConfig("Preview", width=100, style="Hyperlink"),
    }

    # Set up column widths, header values and styles
    for col, conf in column_config.items():
        ws.column_dimensions[col].width = conf.width

        column = ws[f"{col}1"]
        column.value = conf.heading
        column.style = conf.heading_style

    # Add products
    for obj in queryset.order_by("pk"):
        project_photos = obj.productphoto_set.all()[:1]
        url = ""
        if project_photos:
            url = project_photos[0].photo.url

        data = [obj.pk, obj.title, obj.description, obj.price, url]
        ws.append(data)

        row = ws.max_row
        for row_cells in ws.iter_cols(min_row=row, max_row=row):
            for cell in row_cells:
                conf = column_config[cell.column_letter]
                cell.style = conf.style
                if conf.number_format:
                    cell.number_format = conf.number_format

    mimetype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    charset = "utf-8"
    response = HttpResponse(
        content=save_virtual_workbook(wb),
        content_type=f"{mimetype}; charset={charset}",
        charset=charset,
    )
    response["Content-Disposition"] = "attachment; filename=products.xlsx"
    return response 
开发者ID:PacktPublishing,项目名称:Django-3-Web-Development-Cookbook-Fourth-Edition,代码行数:60,代码来源:admin3.py


注:本文中的openpyxl.writer.excel.save_virtual_workbook方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。