'If you want a fixed number of pages (such as from a custom column) then
pass in a value to page_count, otherwise a count will be estimated
using either the fast or accurate algorithm.'

def write_apnx(self, mobi_file_path, apnx_path, method=None, page_count=0):

    import uuid
    apnx_meta = {'guid': str(uuid.uuid4()).replace('-', '')[:8], 'asin': '', 'cdetype': 'EBOK', 'format': 'MOBI_7', 'acr': ''}
    with lopen(mobi_file_path, 'rb') as mf:
        ident = PdbHeaderReader(mf).identity()
        if (ident != 'BOOKMOBI'):
            raise Exception((_('Not    a    valid    MOBI    file.    Reports    identity    of    %s') % ident))
        apnx_meta['acr'] = str(PdbHeaderReader(mf).name())
    with lopen(mobi_file_path, 'rb') as mf:
        mh = MetadataHeader(mf, default_log)
        if (mh.mobi_version == 8):
            apnx_meta['format'] = 'MOBI_8'
        else:
            apnx_meta['format'] = 'MOBI_7'
        if ((mh.exth is None) or (not mh.exth.cdetype)):
            apnx_meta['cdetype'] = 'EBOK'
        else:
            apnx_meta['cdetype'] = str(mh.exth.cdetype)
        if ((mh.exth is None) or (not mh.exth.uuid)):
            apnx_meta['asin'] = ''
        else:
            apnx_meta['asin'] = str(mh.exth.uuid)
    pages = []
    if page_count:
        pages = self.get_pages_exact(mobi_file_path, page_count)
    else:
        try:
            if (method == 'accurate'):
                pages = self.get_pages_accurate(mobi_file_path)
            elif (method == 'pagebreak'):
                pages = self.get_pages_pagebreak_tag(mobi_file_path)
                if (not pages):
                    pages = self.get_pages_accurate(mobi_file_path)
            else:
                raise Exception(('%r    is    not    a    valid    apnx    generation    method' % method))
        except:
            pages = self.get_pages_fast(mobi_file_path)
    if (not pages):
        pages = self.get_pages_fast(mobi_file_path)
    if (not pages):
        raise Exception(_('Could    not    generate    page    mapping.'))
    apnx = self.generate_apnx(pages, apnx_meta)
    with lopen(apnx_path, 'wb') as apnxf:
        apnxf.write(apnx)
        fsync(apnxf)
