def exportAnnoPdf(annotations,outdir,verbose=True): '''Export PDFs <annotations>: dict, keys: docid, values: menotexport.DocAnno obj. <outdir>: str, folder path to save PDFs. Update time: 2018-07-28 20:21:09. ''' faillist=[] num=len(annotations) for ii,annoii in enumerate(annotations.values()): if not annoii.hasfile: continue for fjj,annojj in annoii.file_annos.items(): assert fjj is not None, 'fjj is None' # when side-bar note exists but no pdf. # shouldn't happen if annoii.hasfile is False fnamejj=annojj.filename if verbose: printNumHeader('Exporting PDF:',ii+1,num,3) printInd(fnamejj,4) try: exportPdf(fjj,outdir,annojj,verbose) except: faillist.append(fnamejj) return faillist
def copyPdf(doclist, outdir, verbose=True): '''Copy PDF to target location ''' if not os.path.isdir(outdir): os.makedirs(outdir) faillist = [] num = len(doclist) for ii, docii in enumerate(doclist): pathii = docii['path'] if pathii is None: continue basedir, filename = os.path.split(pathii) targetname = os.path.join(outdir, filename) if not os.path.exists(pathii): faillist.append(pathii) continue if verbose: printNumHeader('Copying file:', ii + 1, num, 3) printInd(filename, 4) try: shutil.copy2(pathii, targetname) except: faillist.append(filename) return faillist
def exportAnnoPdf(annotations, outdir, verbose=True): '''Export PDFs ''' faillist = [] num = len(annotations) for ii, idii in enumerate(annotations.keys()): annoii = annotations[idii] fii = annoii.path fnameii = annoii.filename if verbose: printNumHeader('Exporting PDF:', ii + 1, num, 3) printInd(fnameii, 4) try: exportPdf(fii, outdir, annoii, verbose) except: faillist.append(fnameii) return faillist
def copyPdf(doclist,outdir,verbose=True): '''Copy PDF to target location <doclist>: list of meta data dicts <outdir>: str, path to output folder ''' if not os.path.isdir(outdir): makedirs(outdir) faillist=[] num=len(doclist) for ii,docii in enumerate(doclist): pathii=docii['path'] if pathii is None: continue for jj,pjj in enumerate(pathii): basedir,filename=os.path.split(pjj) targetname=os.path.join(outdir,filename) if not os.path.exists(pjj): faillist.append(pjj) continue if verbose: printNumHeader('Copying file:',ii+1,num,3) printInd(filename,4) try: shutil.copy2(pjj,targetname) except: faillist.append(filename) return faillist
def exportAnno(annodict,outdir,action,verbose=True): '''Export annotations grouped by tags ''' #-----------Export all to a single file----------- if 'm' in action and 'n' not in action: fileout='Mendeley_highlights_by_tags.txt' elif 'n' in action and 'm' not in action: fileout='Mendeley_notes_by_tags.txt' elif 'm' in action and 'n' in action: fileout='Mendeley_annotations_by_tags.txt' abpath_out=os.path.join(outdir,fileout) if os.path.isfile(abpath_out): os.remove(abpath_out) if verbose: printHeader('Exporting all taged annotations to:',3) printInd(abpath_out,4) conv=lambda x:unicode(x) wrapper=TextWrapper() wrapper.width=70 wrapper.initial_indent='' #wrapper.subsequent_indent='\t\t'+int(len('> '))*' ' wrapper.subsequent_indent='\t\t' wrapper2=TextWrapper() wrapper2.width=60 wrapper2.initial_indent='' #wrapper2.subsequent_indent='\t\t\t'+int(len('Title: '))*' ' wrapper2.subsequent_indent='\t\t\t' with open(abpath_out, mode='a') as fout: #----------------Loop through tags---------------- tags=annodict.keys() if len(tags)==0: return tags.sort() #---------------Put @None at the end--------------- if '@None' in tags: tags.remove('@None') tags.append('@None') for tagii in tags: citedictii=annodict[tagii] outstr=u'''\n\n{0}\n# {1}'''.format(int(80)*'-', conv(tagii)) outstr=outstr.encode('ascii','replace') fout.write(outstr) #--------------Loop through cite keys-------------- for citejj, annosjj in citedictii.items(): hljj=annosjj['highlights'] ntjj=annosjj['notes'] outstr=u'''\n\n\t@{0}:'''.format(conv(citejj)) outstr=outstr.encode('ascii','replace') fout.write(outstr) #-----------------Write highlights----------------- if len(hljj)>0: #-------------Loop through highlights------------- for hlkk in hljj: hlstr=wrapper.fill(hlkk.text) title=wrapper2.fill(hlkk.title) outstr=u''' \n\t\t> {0} \t\t\t- Title: {1} \t\t\t- Ctime: {2}'''.format(*map(conv,[hlstr, title,\ hlkk.ctime])) outstr=outstr.encode('ascii','replace') fout.write(outstr) #-----------------Write notes----------------- if len(ntjj)>0: #----------------Loop through notes---------------- for ntkk in ntjj: ntstr=wrapper.fill(ntkk.text) title=wrapper2.fill(ntkk.title) outstr=u''' \n\t\t- {0} \t\t\t- Title: {1} \t\t\t- Ctime: {2}'''.format(*map(conv,[ntstr, title,\ ntkk.ctime])) outstr=outstr.encode('ascii','replace') fout.write(outstr)
def exportAnno(annodict, outdir, action, separate, verbose=True): '''Export highlights and/or notes to txt file <annodict>: dict, keys: PDF file paths, values: [highlight_list, note_list], see doc in _exportAnnoFile(). <outdir>: str, path to output folder. <action>: list, actions from cli arguments. <separate>: bool, True: save annotations if each PDF separately. False: save annotations from all PDFs to a single file. Calls _exportAnnoFile() for core processes. ''' #-----------Export all to a single file----------- if not separate: if 'm' in action and 'n' not in action: fileout = 'Mendeley_highlights.txt' elif 'n' in action and 'm' not in action: fileout = 'Mendeley_notes.txt' elif 'm' in action and 'n' in action: fileout = 'Mendeley_annotations.txt' abpath_out = os.path.join(outdir, fileout) abpath_out = tools.autoRename(abpath_out) if verbose: printInd('Exporting all annotations to:', 3) printInd(abpath_out, 4) #----------------Loop through files---------------- annofaillist = [] num = len(annodict) docids = annodict.keys() for ii, idii in enumerate(docids): annoii = annodict[idii] fii = annoii.path basenameii = os.path.basename(fii) fnameii = os.path.splitext(basenameii)[0] if verbose: printNumHeader('Exporting annos in file', ii + 1, num, 3) printInd(fnameii, 4) #---------Get individual output if needed--------- if separate: if 'm' in action and 'n' not in action: fileout = 'Highlights_%s.txt' % fnameii elif 'n' in action and 'm' not in action: fileout = 'Notes_%s.txt' % fnameii elif 'm' in action and 'n' in action: fileout = 'Anno_%s.txt' % fnameii abpath_out = os.path.join(outdir, fileout) abpath_out = tools.autoRename(abpath_out) if verbose: printInd('Exporting annotations to:', 3) printInd(abpath_out, 4) #----------------------Export---------------------- try: _exportAnnoFile(abpath_out, annoii) except: annofaillist.append(basenameii) continue return annofaillist
def exportAnno(annodict, outdir, action, verbose=True): '''Export annotations grouped by tags ''' #-----------Export all to a single file----------- if 'm' in action and 'n' not in action: fileout = 'Mendeley_highlights_by_tags.txt' elif 'n' in action and 'm' not in action: fileout = 'Mendeley_notes_by_tags.txt' elif 'm' in action and 'n' in action: fileout = 'Mendeley_annotations_by_tags.txt' abpath_out = os.path.join(outdir, fileout) if os.path.isfile(abpath_out): os.remove(abpath_out) if verbose: printHeader('Exporting all taged annotations to:', 3) printInd(abpath_out, 4) conv = lambda x: unicode(x) wrapper = TextWrapper() wrapper.width = 70 wrapper.initial_indent = '' #wrapper.subsequent_indent='\t\t'+int(len('> '))*' ' wrapper.subsequent_indent = '\t\t' wrapper2 = TextWrapper() wrapper2.width = 60 wrapper2.initial_indent = '' #wrapper2.subsequent_indent='\t\t\t'+int(len('Title: '))*' ' wrapper2.subsequent_indent = '\t\t\t' with open(abpath_out, mode='a') as fout: #----------------Loop through tags---------------- tags = annodict.keys() if len(tags) == 0: return tags.sort() #---------------Put @None at the end--------------- if '@None' in tags: tags.remove('@None') tags.append('@None') for tagii in tags: citedictii = annodict[tagii] outstr = u'''\n\n{0}\n# {1}'''.format(int(80) * '-', conv(tagii)) outstr = outstr.encode('ascii', 'replace') fout.write(outstr) #--------------Loop through cite keys-------------- for citejj, annosjj in citedictii.items(): hljj = annosjj['highlights'] ntjj = annosjj['notes'] outstr = u'''\n\n\t@{0}:'''.format(conv(citejj)) outstr = outstr.encode('ascii', 'replace') fout.write(outstr) #-----------------Write highlights----------------- if len(hljj) > 0: #-------------Loop through highlights------------- for hlkk in hljj: hlstr = wrapper.fill(hlkk.text) title = wrapper2.fill(hlkk.title) outstr=u''' \n\t\t> {0} \t\t\t- Title: {1} \t\t\t- Ctime: {2}'''.format(*map(conv,[hlstr, title,\ hlkk.ctime])) outstr = outstr.encode('ascii', 'replace') fout.write(outstr) #-----------------Write notes----------------- if len(ntjj) > 0: #----------------Loop through notes---------------- for ntkk in ntjj: ntstr = wrapper.fill(ntkk.text) title = wrapper2.fill(ntkk.title) outstr=u''' \n\t\t- {0} \t\t\t- Title: {1} \t\t\t- Ctime: {2}'''.format(*map(conv,[ntstr, title,\ ntkk.ctime])) outstr = outstr.encode('ascii', 'replace') fout.write(outstr)
def exportAnno(annodict,outdir,action,separate,verbose=True): '''Export highlights and/or notes to txt file <annodict>: dict, keys: doc ids, values: menotexport.DocAnno objs., <outdir>: str, path to output folder. <action>: list, actions from cli arguments. <separate>: bool, True: save annotations if each PDF separately. False: save annotations from all PDFs to a single file. Calls _exportAnnoFile() for core processes. ''' #-----------Export all to a single file----------- if not separate: if 'm' in action and 'n' not in action: fileout='Mendeley_highlights.txt' elif 'n' in action and 'm' not in action: fileout='Mendeley_notes.txt' elif 'm' in action and 'n' in action: fileout='Mendeley_annotations.txt' abpath_out=os.path.join(outdir,fileout) abpath_out=tools.autoRename(abpath_out) if verbose: printInd('Exporting all annotations to:',3) printInd(abpath_out,4) #----------------Loop through docs---------------- faillist=[] num=len(annodict) docids=annodict.keys() for ii,idii in enumerate(docids): annoii=annodict[idii] if len(annoii.notes)==0 and len(annoii.highlights)==0: continue fnameii=annoii.meta['title'] if verbose: printNumHeader('Exporting annos in file',ii+1,num,3) printInd(fnameii,4) #---------Get individual output if needed--------- if separate: if 'm' in action and 'n' not in action: fileout='Highlights_%s.txt' %fnameii elif 'n' in action and 'm' not in action: fileout='Notes_%s.txt' %fnameii elif 'm' in action and 'n' in action: fileout='Anno_%s.txt' %fnameii abpath_out=os.path.join(outdir,fileout) abpath_out=tools.autoRename(abpath_out) if verbose: printInd('Exporting annotations to:',3) printInd(abpath_out,4) #----------------------Export---------------------- try: # Use custom template formatting if 't' in action: _exportAnnoFileTemplated(abpath_out,annoii) # Use default formatting else: _exportAnnoFile(abpath_out,annoii) except: faillist.append(fnameii) continue return faillist