[Tutor] To write data in two different fonts?

Dave Angel davea at ieee.org
Tue Aug 11 13:03:27 CEST 2009


prasad rao wrote:
> Hello
>      I am wtriting some dat on to a file in which
> headings should be of different size than other data.
> Is it possible?If possible please tell me how to do it.
>
> <code>
>
> def sc(adir):
>       import os,myfiles
>       dest=open('C:/scripts.txt','w')
>       s=myfiles.myfiles(adir)
>       for x in s:
>              if os.path.isfile(x):
>                  sorce=open(x,'r')
>                  data=sorce.readlines()
>                  dest.write(x+'\n','bold')
>
>                  dest.write(('-'*len(x))+'\n')
>                  for l in data:
>                       dest.write(l)
>                  sorce.close()
>            else:pass
>    dest.close()
>
>
>   
>>>> sc('C:\Python26\mscripts')
>>>>         
>
> Traceback (most recent call last):
>   File "<pyshell#69>", line 1, in <module>
>     sc('C:\Python26\mscripts')
>   File "<pyshell#68>", line 9, in sc
>     dest.write(x+'\n','bold')
> TypeError: function takes exactly 1 argument (2 given)
>
> </code>
>
>   
I figure you're asking two questions here.  First is the syntax error.  
All you need to do to fix that is to combine the strings in the write() 
function into a single string, instead of trying to pass two separate 
arguments.  So change from:

                 dest.write(x+'\n','bold')
to

                 dest.write(x + '\n' + 'bold')


The second question was harder for me to understand.  Finally, I noticed 
the subject line, which made it clear what you meant by "different 
size."  I had been assuming you meant "different number of characters."

You're writing a text file here, and plain text files have no notion of 
fonts.  Without knowing the contents/format of the files you're 
combining, and/or the kind of program that's going to be interpreting 
this resulting file, I could only make wild guesses.


If all the source files are html pieces, and the resultant file is to be 
interpreted as html, you could put <b> and </b> around the text you 
wanted to be bold.  But of course, you'd have lots of other formatting 
text, just to be a valid html file.

If the result file is supposed to be rtf  (to be displayed in Wordpad, 
or WinWord), you could put its escape sequences in it.  I'm sure there's 
a spec for rtf out there somewhere in internet land.

If the result file is to be printed by an ancient Epson MX80 printer I  
sort-of recall, you'd put \x0e before the part you wanted bold, and \x0f 
after it.  Or something like that.

If the resultant file is a message for this newsgroup, you could put an 
asterisk before and after the part you want bold.  I think you have to 
do it for each "word," but I've never seen a spec on it.

But if the files are pure printable text files, and the result is to be 
viewable in Notepad, Metapad, or the like, then it can't be done, to the 
best of my knowledge.


DaveA



More information about the Tutor mailing list