2

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

1
  • you can directly make your command write it into csv file, why do you need an export script for the same. once you receive all the urls and class names associated with it, its simply a problem of csv writer for which you can refer https://docs.python.org/2/library/csv.html. you would not need installation of xlsxwriter Commented Oct 20, 2017 at 13:30

1 Answer 1

2

I might be misunderstanding you here, but if you want to export to the xlsx file from the command, then just replace the line that prints the urls to export it to the xlsx file instead.

row = 0
for url in all_urls:
  worksheet.write(row, 0, url)
  row += 1

(You will also need to add bits of the code from export_test.py to the urls.py file in the appropriate places, e.g. open and close the workbook, add a worksheet, import xlsxwriter.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.