I have one command that gives all urls of my project. And i want to export this command to xlsx file. Any ideas? In print message should be url command
Thanks guys in advance.
urls.py (Here command that gives all urls)
from django.conf.urls import RegexURLPattern, RegexURLResolver
from django.core import urlresolvers
from django.core.management import BaseCommand
class Command(BaseCommand):
def add_arguments(self, parser):
pass
def handle(self, *args, **kwargs):
urls = urlresolvers.get_resolver()
all_urls = list()
def func_for_sorting(i):
if i.name is None:
i.name = ''
return i.name
def show_urls(urls):
for url in urls.url_patterns:
if isinstance(url, RegexURLResolver):
show_urls(url)
elif isinstance(url, RegexURLPattern):
all_urls.append(url)
show_urls(urls)
all_urls.sort(key=func_for_sorting, reverse=False)
print('Total urls:', len(all_urls))
print('-' * 220)
for url in all_urls:
print('| {0.regex.pattern:100} | {0.name:50} | {0.lookup_str:70} |'.format(url))
print('-' * 220)
export_test.py (Export command)
import xlsxwriter
# Create a workbook and add a worksheet
workbook = xlsxwriter.Workbook('Test2.xlsx')
worksheet = workbook.add_worksheet()
# Add a bold format
bold = workbook.add_format({'bold': True})
# Write header
worksheet.write('A1', 'URL', bold)
worksheet.write('B1', 'Class', bold)
expanses = (
['TestUrl.com', 'TestClass'],
['TestUrl2.com', 'ExcelClass'],
['TestUrl3.com', 'OrderClass'],
['TestUrl4.com', 'TransferClass'],
)
row = 1
col = 0
for item, cost in (expanses):
worksheet.write(row, col, item)
worksheet.write(row, col + 1, cost)
row += 1
workbook.close()
I think it should be in print message
https://docs.python.org/2/library/csv.html. you would not need installation ofxlsxwriter