XWPF POI adding shape line to top and bottom of every page - apache-poi

How to adding shape line to top and bottom of every document page. Which document have more pages. In image was i give yellow circle. thanks
public static XWPFRun GaRing (XWPFParagraph paragraph, String posisi, String warna) throws Exception {
String boxWidth = "100pt";
String boxHeight = "100pt";
String posLeft = "-10pt";
String posTop = "";
String Path2="";
if (posisi.equals ("atas")) {
posTop = "-10pt";
Path2="m 0,5000 l 20000,0 e"; //atas
}
else {
posTop = "10pt";
Path2="m 0,0 l 20000,5000 e"; //bawah
}
CTGroup ctGroup = CTGroup.Factory.newInstance();
CTShape ctShape = ctGroup.addNewShape();
ctShape.setCoordsize("21600,21600");
ctShape.setPath2(Path2);
ctShape.setStrokecolor(warna);
ctShape.setStyle("position:absolute"
+";top:" + posTop
+";left:" + posLeft
// +";width:" + boxWidth
// +";height:" + boxHeight
);
Node ctGroupNode = ctGroup.getDomNode();
CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode);
XWPFRun run=paragraph.createRun();
CTR cTR = run.getCTR();
cTR.addNewPict();
cTR.setPictArray(0, ctPicture);
return run;
}
index.jsp:
XWPFDocument doc= new XWPFDocument();
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run;
GaRing (paragraph, "atas", "#FF0000");
paragraph.setBorderLeft(Borders.SINGLE);
run=paragraph.createRun();
run.setText("The Body text: ");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
GaRing (paragraph, "bawah", "#FF0000");
FileOutputStream out1 = new FileOutputStream("CreateWordPathShape.docx");
doc.write(out1);
out1.close();

See Apache POI XWPF : How create a group of line? for how to create a line shape in Word.
A method to create a line shape at position of a given paragraph and using a VML path to describe the line could look like so:
private static void createLineShape(XWPFParagraph paragraph, String coordsize, String vmlPath, String strokecolor, String style) throws Exception {
XWPFRun run = paragraph.createRun();
com.microsoft.schemas.vml.CTGroup ctGroup = com.microsoft.schemas.vml.CTGroup.Factory.newInstance();
com.microsoft.schemas.vml.CTShape ctShape = ctGroup.addNewShape();
ctShape.setCoordsize(coordsize);
ctShape.setPath2(vmlPath);
ctShape.setStrokecolor(strokecolor);
ctShape.setStyle(style);
org.w3c.dom.Node ctGroupNode = ctGroup.getDomNode();
CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode);
CTR cTR = run.getCTR();
cTR.addNewPict();
cTR.setPictArray(0, ctPicture);
}
Note the String style describes the size and the position absolute to the given paragraph. For example: "position:absolute;left:0;top:0;width:100pt;height:50pt".
Your requirement "adding shape line to top and bottom of every document page" screams to usage of header and footer. So why not using header and footer?
If you don't have control over the page breaks, then you cannot predict where the pages change. Then top and bottom line shapes would must be in page header and footer.
Following method would do that:
private static void createHeaderAndFooter(XWPFDocument document) throws Exception {
// create header start
XWPFHeader header = document.createHeader(HeaderFooterType.DEFAULT);
XWPFParagraph paragraph = header.getParagraphArray(0);
if (paragraph == null) paragraph = header.createParagraph();
createLineShape(paragraph, "21600,21600", "m 0,21600 l 21600,0 e", "#FF0000", "position:absolute;left:0;top:-14pt;width:100pt;height:50pt");
// create footer start
XWPFFooter footer = document.createFooter(HeaderFooterType.DEFAULT);
paragraph = footer.getParagraphArray(0);
if (paragraph == null) paragraph = footer.createParagraph();
createLineShape(paragraph, "21600,21600", "m 0,0 l 21600,21600 e", "#FF0000", "position:absolute;left:0;top:0;width:100pt;height:50pt");
}
If you have control over the page breaks, you can set the top line shape and the bottom line shape directly in the text flow. Then the line shapes in header and footer are not needed.
Following shows a complete example to show both:
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumbering;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import com.microsoft.schemas.vml.CTGroup;
import com.microsoft.schemas.vml.CTShape;
import org.w3c.dom.Node;
import java.math.BigInteger;
public class CreateWordLineMarkingPagesAndParagraphs {
static String cTAbstractNumDecimalXML =
"<w:abstractNum xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:abstractNumId=\"1\">"
+ "<w:multiLevelType w:val=\"hybridMultilevel\"/>"
+ "<w:lvl w:ilvl=\"0\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"720\" w:hanging=\"360\"/></w:pPr></w:lvl>"
+ "<w:lvl w:ilvl=\"1\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1.%2\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"1440\" w:hanging=\"360\"/></w:pPr></w:lvl>"
+ "<w:lvl w:ilvl=\"2\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1.%2.%3\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"2160\" w:hanging=\"360\"/></w:pPr></w:lvl>"
+ "</w:abstractNum>";
private static BigInteger createNumbering(XWPFDocument document, String abstractNumXML) throws Exception {
CTNumbering cTNumbering = CTNumbering.Factory.parse(abstractNumXML);
CTAbstractNum cTAbstractNum = cTNumbering.getAbstractNumArray(0);
XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
XWPFNumbering numbering = document.createNumbering();
BigInteger abstractNumID = numbering.addAbstractNum(abstractNum);
BigInteger numID = numbering.addNum(abstractNumID);
return numID;
}
private static void setBorderLeftColor(XWPFParagraph paragraph, String rgb) {
if (paragraph.getCTP().getPPr() == null) return; // no paragraph properties = no borders
if (paragraph.getCTP().getPPr().getPBdr() == null) return; // no paragraph border
if (paragraph.getCTP().getPPr().getPBdr().getLeft() == null) return; // no left border
paragraph.getCTP().getPPr().getPBdr().getLeft().setColor(rgb);
}
private static void setTabStopLeaderHYPHEN(double tabStopPosInInches, XWPFParagraph paragraph) {
paragraph.getCTP().getPPr().addNewTabs().addNewTab();
paragraph.getCTP().getPPr().getTabs().getTabArray(0).setVal(
org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabJc.LEFT);
paragraph.getCTP().getPPr().getTabs().getTabArray(0).setLeader(
org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabTlc.HYPHEN);
paragraph.getCTP().getPPr().getTabs().getTabArray(0).setPos(java.math.BigInteger.valueOf(Math.round(tabStopPosInInches * 1440)));
}
private static void setPageSize(XWPFDocument document, double widthInInches, double heightInInches,
double topPageMarginInInches, double bottomPageMarginInInches, double leftPageMarginInInches, double rightPageMarginInInches,
double headerHeightInInches, double footerHeightInInches) {
if (document.getDocument().getBody().getSectPr() == null) document.getDocument().getBody().addNewSectPr();
if (document.getDocument().getBody().getSectPr().getPgSz() == null) document.getDocument().getBody().getSectPr().addNewPgSz();
document.getDocument().getBody().getSectPr().getPgSz().setW(java.math.BigInteger.valueOf(Math.round(widthInInches * 1440)));
document.getDocument().getBody().getSectPr().getPgSz().setH(java.math.BigInteger.valueOf(Math.round(heightInInches * 1440)));
if (document.getDocument().getBody().getSectPr().getPgMar() == null) document.getDocument().getBody().getSectPr().addNewPgMar();
document.getDocument().getBody().getSectPr().getPgMar().setTop(java.math.BigInteger.valueOf(Math.round(topPageMarginInInches * 1440)));
document.getDocument().getBody().getSectPr().getPgMar().setBottom(java.math.BigInteger.valueOf(Math.round(bottomPageMarginInInches * 1440)));
document.getDocument().getBody().getSectPr().getPgMar().setLeft(java.math.BigInteger.valueOf(Math.round(leftPageMarginInInches * 1440)));
document.getDocument().getBody().getSectPr().getPgMar().setRight(java.math.BigInteger.valueOf(Math.round(rightPageMarginInInches * 1440)));
document.getDocument().getBody().getSectPr().getPgMar().setHeader(java.math.BigInteger.valueOf(Math.round(headerHeightInInches * 1440)));
document.getDocument().getBody().getSectPr().getPgMar().setFooter(java.math.BigInteger.valueOf(Math.round(footerHeightInInches * 1440)));
}
private static void createOnePageOfText(XWPFDocument document, BigInteger numIDDecimalList) throws Exception {
XWPFParagraph paragraph = document.createParagraph();
// If you have control over the page breaks, you can set the top line directly in the text flow. Then the top line in footer can be removed.
createLineShape(paragraph, "21600,21600", "m 0,21600 l 21600,0 e", "#FF0000", "position:absolute;left:0;top:-50pt;width:100pt;height:50pt");
paragraph.setAlignment(ParagraphAlignment.BOTH); // alingment justify
// set tab stop at position 6.5 inches
// (right page margin for page size letter and 1 inch left and right page margin)
setTabStopLeaderHYPHEN(6.5, paragraph);
paragraph.setBorderLeft(Borders.SINGLE);
setBorderLeftColor(paragraph, "FF0000");
XWPFRun run = paragraph.createRun();
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.addTab();
for (int i = 0; i < 2; i++) {
paragraph = document.createParagraph();
paragraph.setAlignment(ParagraphAlignment.BOTH); // alingment justify
setTabStopLeaderHYPHEN(6.5, paragraph);
paragraph.setBorderLeft(Borders.SINGLE);
setBorderLeftColor(paragraph, "FF0000");
paragraph.setIndentationLeft((int)Math.round(0.5 * 1440));
run = paragraph.createRun();
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. Additional this paragraph is left indented. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. Additional this paragraph is left indented.");
run.addTab();
paragraph = document.createParagraph();
paragraph.setNumID(numIDDecimalList);
paragraph.setNumILvl(BigInteger.valueOf(0));
paragraph.setAlignment(ParagraphAlignment.BOTH);
setTabStopLeaderHYPHEN(6.5, paragraph);
paragraph.setBorderLeft(Borders.SINGLE);
setBorderLeftColor(paragraph, "FF0000");
run = paragraph.createRun();
run.setText("This is a numbered paragraph which is long enough to go over multiple lines. This is a numbered paragraph which is long enough to go over multiple lines.");
run.addTab();
paragraph = document.createParagraph();
paragraph.setNumID(numIDDecimalList);
paragraph.setNumILvl(BigInteger.valueOf(0));
paragraph.setAlignment(ParagraphAlignment.BOTH);
setTabStopLeaderHYPHEN(6.5, paragraph);
paragraph.setBorderLeft(Borders.SINGLE);
setBorderLeftColor(paragraph, "FF0000");
run=paragraph.createRun();
run.setText("This is another numbered paragraph which is long enough to go over multiple lines. This is another numbered paragraph which is long enough to go over multiple lines.");
run.addTab();
paragraph = document.createParagraph();
paragraph.setAlignment(ParagraphAlignment.BOTH);
setTabStopLeaderHYPHEN(6.5, paragraph);
paragraph.setBorderLeft(Borders.SINGLE);
setBorderLeftColor(paragraph, "FF0000");
run = paragraph.createRun();
run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
run.addTab();
}
paragraph = document.createParagraph();
// If you have control over the page breaks, you can set the bottom line directly in the text flow. Then the bottom line in footer can be removed.
createLineShape(paragraph, "21600,21600", "m 0,0 l 21600,21600 e", "#FF0000", "position:absolute;left:0;top:0;width:100pt;height:50pt");
}
private static void createLineShape(XWPFParagraph paragraph, String coordsize, String vmlPath, String strokecolor, String style) throws Exception {
XWPFRun run = paragraph.createRun();
com.microsoft.schemas.vml.CTGroup ctGroup = com.microsoft.schemas.vml.CTGroup.Factory.newInstance();
com.microsoft.schemas.vml.CTShape ctShape = ctGroup.addNewShape();
ctShape.setCoordsize(coordsize);
ctShape.setPath2(vmlPath);
ctShape.setStrokecolor(strokecolor);
ctShape.setStyle(style);
org.w3c.dom.Node ctGroupNode = ctGroup.getDomNode();
CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode);
CTR cTR = run.getCTR();
cTR.addNewPict();
cTR.setPictArray(0, ctPicture);
}
private static void createHeaderAndFooter(XWPFDocument document) throws Exception {
// create header start
XWPFHeader header = document.createHeader(HeaderFooterType.DEFAULT);
XWPFParagraph paragraph = header.getParagraphArray(0);
if (paragraph == null) paragraph = header.createParagraph();
createLineShape(paragraph, "21600,21600", "m 0,21600 l 21600,0 e", "#FF0000", "position:absolute;left:0;top:-14pt;width:100pt;height:50pt");
// create footer start
XWPFFooter footer = document.createFooter(HeaderFooterType.DEFAULT);
paragraph = footer.getParagraphArray(0);
if (paragraph == null) paragraph = footer.createParagraph();
createLineShape(paragraph, "21600,21600", "m 0,0 l 21600,21600 e", "#FF0000", "position:absolute;left:0;top:0;width:100pt;height:50pt");
}
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
BigInteger numIDDecimalList = createNumbering(document, cTAbstractNumDecimalXML);
XWPFParagraph paragraph = document.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run=paragraph.createRun();
run.setText("The header line of the whole document.");
run.addBreak();
run.setText("blah, blubb, barr...");
// This method creates exactly one page of text.
createOnePageOfText(document, numIDDecimalList);
// add page break
document.createParagraph().createRun().addBreak(BreakType.PAGE);
createOnePageOfText(document, numIDDecimalList);
// If you don't have control over the page breaks, then you cannot predict where the pages change. Then top and bottom lines would must be in page header and footer.
//createHeaderAndFooter(document);
// set page size letter format (8.5 x 11 inches) and 1 inch top, bottom, left and right page margin and .5 inch header and footer height
setPageSize(document, 8.5, 11d, 1d, 1d, 1d, 1d, .5d, .5d);
FileOutputStream out = new FileOutputStream("./CreateWordLineMarkingPagesAndParagraphs.docx");
document.write(out);
out.close();
document.close();
}
}

Related

I was able to search for the word and print the grid with dots and word but there is a extra line of dots in printing after it

I have a text file of grid of letters from where I have to find a word
such as
odsdsnslryy
ecajjgigqgy
fqlzfbzedme
wqogkvatqjh
lsjmvdkmqlu
rhnrbzrnfcg
euxwmyleqei
ezzamtwmgst
examplejpzh
fkhygikqjuc
igzynopmxrc
faexqjljvom
wtxtehloipx
example
I have to find the word example from the grid and reprint the grid with the word such as
The code I wrote was able to print it however the line of dots after the line with word is printing beside that rather than printing it after the line finish.
the code i wrote
row_num = len(grid)
col_num = len(grid[0])
for elem in grid:
for word in words:
if word not in elem:
print(col_num * '.')
else:
len_word = len(word)
for i in range(len(elem)):
if elem[i:len_word] == word:
print(word, end = '')
else:
print('.', end = '')
and it's printing is
Also please let me know what change I have to make if I want to search the word from right to left
Is this what you want? Where sample.txt is the "grid" of words copied as-is from your question.
with open("sample.txt") as f:
word = "example"
lines = [l.strip() for l in f.readlines()]
longest_line = len(max(lines))
grid = "\n".join(
"." * len(line) if word not in line
else f"{word}{'.' * (longest_line - len(word))}"
for line in lines
)
print(grid)
Output:
...........
...........
...........
...........
...........
...........
...........
...........
example....
...........
...........
...........
...........

how to return BOLD characters in python terminal

I'm trying to return a string as bold string.
I define a class called color
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
when I type print(color.BOLD + 'Hello World !' + color.END) in the python console,it is print the Hello World in bold
But when I just type color.BOLD + 'Hello World !' + color.END in the terminal it is returning '\x1b[1mHello World !\x1b[0m'
I want to know how can I returning in bold characters?
It's because when you type something that returns value in python console, it will automatically print its string representation. That means every unprintable escape characters will be printed as \<something>.
For example, newline has representation \n, carriage return "\r" etc. . If you pass escape character to print function, the escape character will be printed to console as it should be and terminal will treat it as signal for something (e.g. "go to new line" or "change color"), but its representation is actually character "\" followed by other characters.

Printing Extended ASCII characters in Python

I'd like to create menu boxes in Python with the following ascii characteres:
ASCII code 200 = ╚ ( Box drawing character double line lower left corner )
ASCII code 201 = ╔ ( Box drawing character double line upper left corner )
ASCII code 202 = ╩ ( Box drawing character double line horizontal and up )
ASCII code 203 = ╦ ( Box drawing character double line horizontal down )
ASCII code 204 = ╠ ( Box drawing character double line vertical and right )
ASCII code 205 = ═ ( Box drawing character double horizontal line )
ASCII code 206 = ╬ ( Box drawing character double line horizontal vertical )
However it seems that these are not supported. When I use chr() the system prints something totally different.
Do you have any advise on how to print the mentioned characters using Python 3?
Thank you.
It seems to work with string literals:
>>> symbs = [u'\u255a', u'\u2554', u'\u2569', u'\u2566', u'\u2560', u'\u2550', u'\u256c']
>>> for sym in symbs:
... print(sym)
...
╚
╔
╩
╦
╠
═
╬
This appears to work on all platforms I've tried it on, Windows 7, Mac OS , Linux, etc.
Codes gotten from http://svn.python.org/projects/stackless/trunk/Lib/encodings/cp720.py
Hope this helps.
Follow-up thanks to Dave Edwards ...
class TableBorder:
def __init__ (self, top_left, top_split, top_right,
mid_left, mid_split, mid_right,
low_left, low_split, low_right,
horizontal, vertical):
self.top_left = top_left
self.top_split = top_split
self.top_right = top_right
self.mid_left = mid_left
self.mid_split = mid_split
self.mid_right = mid_right
self.low_left = low_left
self.low_split = low_split
self.low_right = low_right
self.horizontal = horizontal
self.vertical = vertical
Borders0 = TableBorder ('+', '+', '+', '+', '+', '+', '+', '+', '+', '-', '|')
Borders1 = TableBorder (u'\u250c',u'\u252C',u'\u2510',u'\u251C',u'\u253C',u'\u2524',u'\u2514',u'\u2534',u'\u2518',u'\u2500',u'\u2502')
Borders2 = TableBorder (u'\u2554',u'\u2566',u'\u2557',u'\u2560',u'\u256C',u'\u2563',u'\u255a',u'\u2569',u'\u255d',u'\u2550',u'\u2551')
def draw_box (width, height, box):
span = width-2
line = box.horizontal * (span)
print (box.top_left + line + box.top_right)
body = box.vertical + (' '*span) + box.vertical
for _ in range (height-1):
print (body)
print (box.low_left + line + box.low_right)
draw_box (20, 10, Borders1)

Find word position (not character position) in a string

I am sorting the words in a given paragraph from a text file. I have the line numbers and the number of occurrence of each word sorted out. However, I am having a bit of a trouble trying to get the word position number per line. I am using object list to sort the strings.
For example:
Given line
I have a question about the word position number
Output should be
question - line 1 position 4
about - line 1 position 5
position - line 1 position 8
Any useful suggestions how I should go about this? Below is my code.
public void visit() {
ListNode p = lines.getFirstNode();
if (word.length() < 4) {
so.output(word + "\t\t\t\t" + count + "\t\t\t\t");
while (p != null) {
so.output(p.getInfo() + " ");
p = p.getNext();
}
so.output("\n");
...
you're looking for
String line = "I have a question about the word position number";
String[] lineArray = line.split(" ");
int wordPos = Arrays.asList(lineArray).indexOf("question");
just remember that wordPos here is going to output to 3, because arrays start counting from 0, so you'll probably want to add 1 to it when you're formatting your output string because users won't understand that.

How do I make all vowels in the second half of a string uppercase?

I am trying to make all vowels in the second half of a string uppercase. This is what I did so far, I can't seem to get the result I want.
#Ask user for word
word = str(input("Please enter a word: "))
#Count word length
wordLen = len(word)
#Count half of word length
halfWordLen = int (wordLen/2)
#Obtain 1st and 2nd half of the word
firstHalf = word[:halfWordLen]
secondHalf = word[halfWordLen:]
#Determine vowels
vowels = set(['a','e','i','o','u'])
#Iterate through 2nd half to find vowel.
#Then, uppercase the vowels, and display new word.
for char in secondHalf:
if char in vowels:
newWord = firstHalf + secondHalf.replace(char,(char.upper()))
print ("The new word is: ",newWord)
Results:
Please enter a word: abrasion
The new word is: abrasIon
The new word is: abrasiOn
It's supposed to be:
Please enter a word: abrasion
The new word is: abrasIOn
There are two issues with your code. First, when you are replacing the vowel in your second half, you are only doing it temporarily. You need to perform that operation in a separate line and save it in the second half variable.
Also, you are printing the your temporary result each time you go through the loop. If you only want it to only print once, just decrease the level of indent so that it is outside the loop. Here is how I would restructure it.
for char in secondHalf:
if char in vowels:
secondHalf = secondHalf.replace(char,(char.upper()))
newWord = firstHalf + secondHalf
print ("The new word is: ",newWord)

Resources