1

I am trying to change the output fonts in python. I have read you can use Tkinter package but it is too complicated for me.

I am trying to parse two documents, one plain text and the other one in italics. I want the output to alternate lines from each document, plain and italic.

Currently the program displays the first line plain, the second in italics and afterwards it displays all in italics.

I would like to know if there is any way to change/reset the fonts format in the console output.

Thanks,

Here it is the program. Basically I do not know what to do to get output in different formats: one in plain text and the other in italics. I am running it in Python 3 under windows.

L1 = open ('vera.txt','r',-1,'utf-8')
L2 = open ('vera_en2.txt','r',-1,'utf-8')
O = open ('output.txt','w',-1,'utf-8')
for line in L1:
    line2 = L2.readline()
    print(line, end='')
    print(line2)
    O.write(line)
    O.write(line2)
    O.write("\n")
L1.close()
L2.close()
O.close()
Junuxx
  • 14,011
  • 5
  • 41
  • 71
rodbs
  • 185
  • 1
  • 4
  • 12
  • What's your code so far? – TyrantWave Feb 27 '13 at 09:56
  • @TyrantWave that's a bit of a lazy question! – Joe Feb 27 '13 at 10:14
  • 1
    How are you handling inputs and outputs? Displaying on the screen? Reading and writing rich text files or document formats? If you really mean TTY console, which platform? – Joe Feb 27 '13 at 10:14
  • Sounds like you're using something like curses or colorama to get italic text in the console? Please explain. Since you already managed to get some lines of italic text, it might be as simple as changing a `if linenumber > 1: ...` to `if linenumber % 2: ...`, but we can't say without seeing the relevant code. – Junuxx Feb 27 '13 at 10:31
  • 2
    @Joe: I think TyrantWave's question is sensible and to the point! – Junuxx Feb 27 '13 at 10:32
  • @Junuxx it's a useful question in many circumstances but I think it was obvious from reading the question that the questioner didn't have any meaningful amount of code. – Joe Feb 27 '13 at 10:41
  • @Joe: I disagree, the "Currently the program..." sentence seems to indicate he's halfway there. – Junuxx Feb 27 '13 at 10:53
  • I was wrong. I must have misread it. Sorry. – Joe Feb 27 '13 at 10:53

1 Answers1

1

It seems likely that there is an ANSI escape code in your vera_en2.txt that sets the font to italic. Open the file or print the repr() of the first line of that file to verify this.

Apparently you are working in an ANSI compatible terminal already, or you wouldn't be seeing italic text. So, you should be able to reset the font with print("\033[0m"), and enable italics again with print("\033[3m").

If this all seems too esoteric, I recommend using colorama.

Edit:

Ah, so you want to write an RTF file with some italic text. And your .txt files aren't actually .txt files. That's completely different from what I explained above.

By analyzing the plain text of an actual rtf I came up with this small example. It generate a small rtf file with normal, italic and bold words; \i and \b are used to toggle these formats.

# open file
myrtf = open('myrtf.rtf', 'w ')

# write some necessary header info
myrtf.write(r'{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}')

# write the actual text - notice I'm using raw strings (r'')
myrtf.write(r'normal \i italic \i0 \b bold \b0 normal\n')

# write end of file stuff
myrtf.write(r'}\n\x00')

# close file
myrtf.close()

Also, answering the other question; getting italics in the console might be difficult, most terminals just don't support it.

To adapt the above code sample to the problem in your question:

L1 = open ('file1.txt','r')
L2 = open ('file2.txt','r')
O = open ('output.rtf','w')

# write header
O.write(r'{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}')

for line in L1:
    line2 = r"\i  " + L2.readline() + r" \i0\line\par" # make text from L2 italic
    print(line, end='')
    print(line2)
    O.write(line)
    O.write(line2)

# close rtf
O.write(r'}\n\x00')

L1.close()
L2.close()
O.close()

Another addition:

Based on our discussion in the comments, here's one way to do it by writing a html file instead of rtf:

L1 = open ('file1.txt','r')
L2 = open ('file2.txt','r')
O = open ('output.html','w')

# write header
O.write(r'<html><head><title>Foobar</title></head><body><table>')

for line in L1:
    line2 = r"<i>" + L2.readline() + r"</i></br>" # make text from L2 italic
    O.write("<tr>")
    O.write("<td>%s</td>" % line)
    O.write("<td>%s</td>" % line2)
    O.write("</tr>\n")

# footer
O.write(r'</table></body></html>')

L1.close()
L2.close()
O.close()

Here you can see the result when file1.txt and file2.txt are Old English and modern versions of a verse from Beowulf.

Junuxx
  • 14,011
  • 5
  • 41
  • 71
  • It doesn't work. Sorry, I think I am mixing things up. I am using the Windows terminal and I don´t get any italics. I only get italics if reading from rtf files and writing to rtf files. What I would like to know is two things: 1. How can I get italics in the console? what console do I have to use? 2. How can I write on a file, changing from plain to italics and viceversa? – rodbs Feb 27 '13 at 15:04
  • @Rodrigo: Thanks for the additional info, now it is much clearer what you want to do. See my updated answer above. – Junuxx Feb 27 '13 at 15:39
  • I do not understand what you say about "notice I'm using raw strings (r'')". How do I put a variable in? Thanks. – rodbs Feb 27 '13 at 17:45
  • @Rodrigo: I used [raw strings](http://docs.python.org/2/reference/lexical_analysis.html#string-literals), those have an r before the first quote. You could also use the normal string syntax, but then every backslash needs to be escaped, like "normal \\i italic \\i0". About variables: I'll edit that in. – Junuxx Feb 27 '13 at 18:06
  • It now prints the text almost correctly. I have some text in French, and this "à" is printed incorrectly. Besides everything is printed in a single line. I have tried to put \n and r'\n' everywhere but it does not work. – rodbs Feb 27 '13 at 19:00
  • I fixed the issue with everything ending up in one line; rtf uses `\line\par`. For the unicode issue, please refer to [**this**](http://stackoverflow.com/questions/9908647/outputting-unicode-text-to-an-rtf-file-in-python) question. But altogether I think you might be better off writing HTML files instead of rtf. – Junuxx Feb 27 '13 at 21:32
  • 1
    You should have told the HTML stuff before!! :) So how can I do this in HTML? Should I change the headings? Is there an easy way? Thanks!! – rodbs Feb 28 '13 at 15:20