Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def report_to_xlsx(self, path):
workbook = Workbook(path)
form = ExcelFormat(workbook)
summarysheet = workbook.add_worksheet('Summary')
row = 0
col = 0
col += 1
print (TEST_PLATFORM + ('Total',))
for platform in TEST_PLATFORM + ('Total',):
start_platfom_col = col
for tctype in TESTCASE_TYPES + ('API TC',):
summarysheet.write(row + 1, col, tctype, form.title)
col += 1
def report_to_xlsx(self, path):
workbook = Workbook(path)
form = ExcelFormat(workbook)
summarysheet = workbook.add_worksheet('Summary')
row = 0
col = 0
summarysheet.merge_range(row, col, row+3, col, '', form.cell)
col += 1
for platform in TEST_PLATFORM:
if not platform in self.summary:
continue
row += 1
platform_col = col
firstLine = True
else:
columnNames = []
columnLength = input(
'Enter number of columns from the CSV file that you want to import: ')
print('Rows with column length less than or greater than',
columnLength, 'will be ignored')
print('Enter column names to be entered in the XLS document: ')
for x in range(1, int(columnLength) + 1):
cName = input('column name #' + str(x) + ': ')
columnNames.append(cName)
# Write the column name to the Excel Sheet
xlFileName = input(
'Enter the name by which you want the XLS file to be saved: ')
workbook = xl.workbook.Workbook(xlFileName + '.xlsx')
worksheet = workbook.add_worksheet()
cell_format = workbook.add_format({'bold': True})
worksheet.set_row(0, 15, cell_format)
worksheet.write_row('A1', columnNames)
# Write data from CSV to XLS
for row in openFile:
if firstLine:
firstLine = False
continue
elif not len(row) < int(columnLength) or len(row) > int(columnLength):
worksheet.write_row('A' + str(rowNo), row)
rowNo += 1
totalLines += 1
else:
skipped += 1
def get_xlsx_writer(self, response, **kwargs):
return Workbook(response, {'in_memory': True})
#######################################################################
#
# An example of a Combined chart in XlsxWriter.
#
# Copyright 2013, John McNamara, jmcnamara@cpan.org
#
from xlsxwriter.workbook import Workbook
workbook = Workbook('chart_combined.xlsx')
worksheet = workbook.add_worksheet()
# Add a format for the headings.
bold = workbook.add_format({'bold': True})
# Add the worksheet data that the charts will refer to.
headings = ['Number', 'Batch 1', 'Batch 2']
data = [
[2, 3, 4, 5, 6, 7],
[10, 40, 50, 20, 10, 50],
[30, 60, 70, 50, 40, 30],
]
worksheet.write_row('A1', headings, bold)
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
def generate_xlsx(request, pk):
"""View function to generate a xlsx report for the specified report."""
try:
report_instance = Report.objects.get(pk=pk)
# Ask Spenny to make us a report with these findings
output_path = os.path.join(settings.MEDIA_ROOT, report_instance.title)
evidence_path = os.path.join(settings.MEDIA_ROOT)
template_loc = None
spenny = reportwriter.Reportwriter(
report_instance,
output_path,
evidence_path,
template_loc)
output = io.BytesIO()
workbook = Workbook(output, {'in_memory': True})
spenny.generate_excel_xlsx(workbook)
output.seek(0)
response = HttpResponse(
output.read(),
content_type='application/application/vnd.openxmlformats-'
'officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename=report.xlsx'
output.close()
return response
except Exception as e:
messages.error(request, 'Failed to generate the Xlsx report: {}'.format(e),
extra_tags='alert-danger')
return HttpResponseRedirect(request.META['HTTP_REFERER'])
# string widths.
min_width = 0
# Check if it the string is the largest we have seen for this column.
string_width = excel_string_width(string)
if string_width > min_width:
max_width = self.max_column_widths.get(col, min_width)
if string_width > max_width:
self.max_column_widths[col] = string_width
# Now call the parent version of write_string() as usual.
return super(MyWorksheet, self).write_string(row, col, string,
cell_format)
class MyWorkbook(Workbook):
"""
Subclass of the XlsxWriter Workbook class to override the default
Worksheet class with our custom class.
"""
def add_worksheet(self, name=None):
# Overwrite add_worksheet() to create a MyWorksheet object.
# Also add an Worksheet attribute to store the column widths.
worksheet = super(MyWorkbook, self).add_worksheet(name, MyWorksheet)
worksheet.max_column_widths = {}
return worksheet
def close(self):
# We apply the stored column widths for each worksheet when we close
def create_workbook(self, options = None):
if options and "defaultDateFormat" in options:
default_date_format = options["defaultDateFormat"]
else:
default_date_format = "yyyy-mm-dd"
self.workbook = Workbook(self.filename, { "default_date_format": default_date_format, "constant_memory": True })
self.dump_record("open", self.filename)
if options and "properties" in options:
self.workbook.set_properties(options["properties"])
"""
Generate a XLSX file for download
"""
# Query for schema
schema = get_object_or_404(Schema, random_id = schema_id)
single_tab = request.GET.get('singleTab')
try:
# Generate output string
output = StringIO.StringIO()
# Create workbook
book = Workbook(output, {'in_memory': True})
# Set up bold format
bold = book.add_format({'bold': True})
# List of unique names, as 31 characters is the limit for an object
# and the worksheets names must be unique
unique_names = []
unique_count = 1
# This puts the objects on different worksheets
if not single_tab:
# create a sheet for each object
for obj in schema.sorted_objects_api():
# strip api name