本文整理汇总了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))
示例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)]
示例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
示例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)
示例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
示例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