def _doc_is_empty(self): """Returns true if the current document is practically empty. This may not recognize some objects such as frames. However it does work for tables. """ oTextEnum = iteruno.byEnum(self.unoObjs.text) paragraphs = list(oTextEnum) parCount = len(paragraphs) logger.debug("Found %d paragraphs in current doc.", parCount) if parCount > 2: return False for oPar in paragraphs: oParEnum = iteruno.byEnum(oPar) par_elems = list(oParEnum) parElemCount = len(par_elems) logger.debug("Found %d paragraph elements.", parElemCount) if parElemCount > 2: return False oVC = self.unoObjs.viewcursor oVC.gotoStart(False) CHARS_REQUIRED = 10 for dummy in range(CHARS_REQUIRED): if not oVC.goRight(1, False): return True logger.debug("Document has at least %d characters.", CHARS_REQUIRED) return False
def textSectionsOfPar(self, oPar, textSectionList=None): """Recursively enumerate paragraphs, tables and frames. Tables may be nested. """ if textSectionList is None: textSectionList = [] if oPar.supportsService("com.sun.star.text.Paragraph"): for oTextPortion in iteruno.byEnum(oPar): if oTextPortion.TextPortionType == "Text": # TextPortions include the TextRange service. logger.debug("simple text portion") textSectionList.append(oTextPortion) elif oTextPortion.TextPortionType == "Frame": logger.debug("Frame text portion") oFrameEnum = oTextPortion.createContentEnumeration( "com.sun.star.text.TextFrame") # always only 1 item? for oFrame in iteruno.fromEnum(oFrameEnum): self.textSectionsOfPar(oFrame, textSectionList) elif oPar.supportsService("com.sun.star.text.TextTable"): oTable = oPar logger.debug("table %s", oTable.getName()) self.unoObjs.controller.select(oTable) # go to first cell for cellName in oTable.getCellNames(): logger.debug("cell %s:%s", oTable.getName(), cellName) oCell = oTable.getCellByName(cellName) for oPar2 in iteruno.byEnum(oCell): self.textSectionsOfPar(oPar2, textSectionList) elif oPar.supportsService("com.sun.star.text.TextFrame"): oFrame = oPar logger.debug("frame %s", oFrame.getName()) for oPar2 in iteruno.byEnum(oFrame): self.textSectionsOfPar(oPar2, textSectionList) return textSectionList
def footnotes(self): textSections = [] logger.debug("looking for footnotes") footnotes = self.unoObjs.document.getFootnotes() endnotes = self.unoObjs.document.getEndnotes() for notes in (footnotes, endnotes): for oNote in iteruno.byIndex(notes): for oPar in iteruno.byEnum(oNote): if oPar.supportsService("com.sun.star.text.Paragraph"): for oTextPortion in iteruno.byEnum(oPar): if oTextPortion.TextPortionType == "Text": # TextPortions include the TextRange service. textSections.append(oTextPortion) return textSections
def doSearch(self): """ Given a char style name, finds the next ref of that style. Returns the entire string that uses the style. If nothing is found, returns an empty string. """ logger.debug("doSearch %s", self.charStyleName) if self.startFromBeginning: logger.debug("going to beginning") oLCurs = self.unoObjs.text.createTextCursor() oLCurs.gotoStart(False) self.oVC.gotoRange(oLCurs, False) self.oVC.collapseToEnd() # make sure nothing is selected if self.oVC.TextFrame: # Need to get outside of the frame logger.debug("escaping from text frame") for dummy in range(2): self.unoObjs.dispatcher.executeDispatch( self.unoObjs.frame, ".uno:Escape", "", 0, ()) ## Look through the document for the character style self.vcloc = VCLocation(self.unoObjs, self.startFromBeginning) for oPar in iteruno.byEnum(self.unoObjs.text): result = "" if oPar.supportsService("com.sun.star.text.Paragraph"): result = self._searchPara(oPar) elif oPar.supportsService("com.sun.star.text.TextTable"): result = self._searchTable(oPar) if result: return result logger.debug("returning empty string") return ""
def changeParaStyle(self, oCurs): """Goes to the paragraph and sets the style. Changes any directly formatted font name and size to the default. """ logger.debug(util.funcName('begin')) oCursDbg = oCurs.getText().createTextCursorByRange(oCurs.getStart()) oCursDbg.gotoRange(oCurs.getEnd(), True) logger.debug("oCursText = '%s'", oCursDbg.getString()) firstCellName = "" # enumerate current paragraph for oTextElem in iteruno.byEnum(oCurs): logger.debug("oTextElem %s", oTextElem.getString()) if oTextElem.TextTable: curCurs = oTextElem.getText().createTextCursorByRange( oTextElem.getStart()) if curCurs is None: # This happens after the first cell; I don't know why. curCellName = "none" else: curCurs.goRight(0, False) curCellName = curCurs.Cell.CellName logger.debug("cell %s", curCellName) if firstCellName == "": firstCellName = curCellName elif curCellName != firstCellName: # Somehow we've gotten out of the cell logger.debug( "moved out of %s to %s", firstCellName, curCellName) break if oTextElem.supportsService("com.sun.star.text.Paragraph"): curStyleName = oTextElem.getPropertyValue(self.styleType) if curStyleName != self.newStyleName: logger.debug("Setting style %s", self.newStyleName) oTextElem.setPropertyValue( self.styleType, self.newStyleName) self.numStyleChanges += 1 for oTextPortion in iteruno.byEnum(oTextElem): for propName in ("CharFontName", "CharHeight"): if (oTextPortion.getPropertyState(propName) == DIRECT_VALUE): oTextPortion.setPropertyToDefault(propName) logger.debug("setToDefault %s", propName) self.clearFont(oCurs) logger.debug(util.funcName('end'))
def textSectionsOfPar(self, oPar, textSectionList=None): """Recursively enumerate paragraphs, tables and frames. Tables may be nested. """ if textSectionList is None: textSectionList = [] for oTextPortion in iteruno.byEnum(oPar): if oTextPortion.TextPortionType == "Text": # TextPortions include the TextRange service. logger.debug("simple text portion") textSectionList.append(oTextPortion) return textSectionList
def textSectionsForShapeEnum(self, oShapeEnumerator): """Get text sections for all paragraphs that are enumerated by the given object. Do not pass a cursor inside a table as the oShapeEnumerator, because it will work but the entire table or paragraph will be enumerated, not just the selection. Instead use addRangesForCursor(). """ textSections = [] i = 0 for oPar in iteruno.byEnum(oShapeEnumerator): i += 1 logger.debug("par %d: %s", i, oPar.ImplementationName) textSections += self.textSectionsOfPar(oPar) return textSections
def _searchPara(self, oPar): """ Searches a normal (i.e. non-table) paragraph. """ logger.debug("looking at para") if not self.vcloc.parAfter(oPar): return "" for oSection in iteruno.byEnum(oPar): if oSection.TextPortionType == "Text": if oSection.CharStyleName == self.charStyleName: logger.debug("Found style %s", self.charStyleName) result = oSection.getString() if result: self.oVC.gotoRange(oSection.getStart(), False) self.oVC.gotoRange(oSection.getEnd(), True) #logger.debug("returning a string") logger.debug("returning string '%s'", result) return result return ""