def process_okular_xml(xml_path): xml = open(xml_path).read() soup = BeautifulStoneSoup(xml) ls_ano = [] for page in soup.findChildren("page"): ls_annotation = page.findChildren("annotation") if not ls_annotation: print("no annotations found") return for ano in ls_annotation: OKA = OkularAnnotation(ano) ls_xml.append(OKA.process()) ls_ano.append(OKA) return ls_ano
def write_okular_annotation(source_pdf, okular_xml, output_dir): print("#" * 80) print("OUTPUTTING TO: %s" % output_dir) print("#" * 80) pdfr = pyPdf.PdfFileReader(open(source_pdf, "rb")) _xzero, _yzero, DOC_WIDTH, DOC_HEIGHT = pdfr.getPage(0)['/MediaBox'] pdfr.stream.close() # duno if this frees anything at all DIR_ANNOTATION = '.' FILE_OUTPUT_TEMPLATE = "p%s.svg" dc_page_annotation = {} soup = BeautifulStoneSoup(okular_xml) for page in soup.findChildren("page"): page_num = int(page.get("number")) ls_annotation = page.findChildren("annotation") if not ls_annotation: print("no annotations found") return ls_xml = [] for ano in ls_annotation: OKA = OkularAnnotation(ano) ls_xml.append(OKA.process()) svg_out = wrap_into_svg(DOC_WIDTH, DOC_HEIGHT, "\n".join(ls_xml)) open(FILE_OUTPUT_TEMPLATE % page_num, "w").write(svg_out) dc_page_annotation[page_num] = FILE_OUTPUT_TEMPLATE % page_num #print(svg_out) #print("\n------\n\n") PDFAP = PDFAnnotationProcessor.PDFAnnotationProcessor( \ source_pdf, DIR_ANNOTATION, dc_page_annotation) PDFAP.extract_all_annotation(output_dir)
def RetrieveForecast(self, days=1, unicode=True, low=True, high=True, conditions=True, sunrise=False, sunset=False, moon_percent=False, moon_age=False): """ Parse out the forecast from the information obtained by GetXMLForecast(). Return a dictionary like the following: { 1: { "high": { "fahrenheit": 27, "celsius": -3 }, "low": { "fahrenheit": 16, "celsius": -9 }, "conditions": "Mostly Cloudy", } } If unicode is True, display °'s with temperatures, but temperature is *always* returned as a string. """ urlobject = self.GetXMLForecast() soup = BeautifulStoneSoup(urlobject) if days > 6: raise "You've requested too many days. Days must be <= 6." key = days + 2 output_dict = {} for day in range(2, key): loopday = soup.findChildren("forecastday")[day] # Initialize the dictionary. day_dict = output_dict[int(loopday.period.string)] = {} day_dict["epoch"] = int(loopday.date.epoch.string) if low == True: lowf = int(loopday.low.fahrenheit.string) lowc = int(loopday.low.celsius.string) day_dict["low"] = { "fahrenheit": str(lowf), "celsius": str(lowc) } if unicode == True: day_dict["low"]["fahrenheit"] += "°" day_dict["low"]["celsius"] += "°" if high == True: highf = int(loopday.high.fahrenheit.string) highc = int(loopday.high.celsius.string) day_dict["high"] = { "fahrenheit": str(highf), "celsius": str(highc) } if unicode == True: day_dict["high"]["fahrenheit"] += "°" day_dict["high"]["celsius"] += "°" if conditions == True: conditions = str(loopday.conditions.string) day_dict["conditions"] = conditions return output_dict