Writing to MS Word.. Help?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2009
Posts: 17
Reputation: SuperMetroid is an unknown quantity at this point 
Solved Threads: 0
SuperMetroid's Avatar
SuperMetroid SuperMetroid is offline Offline
Newbie Poster

Writing to MS Word.. Help?

 
0
  #1
Aug 9th, 2009
Why don't either of these implementations actually write to the file?

Code v1:
  1. txtfile = open('C:/Python31/SKUs.txt', 'w')
  2.  
  3. with txtfile:
  4. num = 817
  5. while num <= 820:
  6. x = "G" + str(num)
  7. print(str(x), end=" ")
  8. num += 1
  9.  
  10. txtfile.close()

Code v2:
  1. txtfile = open('C:/Python31/SKUs.txt', 'w')
  2.  
  3. num = 817
  4. while num <= 820:
  5. x = "G" + str(num)
  6. print(str(x), end=" ", file = txtfile)
  7. num += 1
  8.  
  9. print("test", file = txtfile)
  10.  
  11. txtfile.close()

Also, I could use some further help.

Here's what I want each page to look like (note the big font size of the SKUs relative to the page size, and the spacing):
  1.  
  2. -----------------------------
  3. | G817 G818 G819 |
  4. | | <-- page 1
  5. | |
  6. | |
  7. |___________________________|

  1.  
  2. -----------------------------
  3. | G817 G818 G819 |
  4. | | <-- page 2
  5. | | (and so on, until G10000)
  6. | |
  7. |___________________________|

I don't know how to make the font-size huge, or to achieve this spacing through Python code. Nor do I know how to create page breaks. If someone could help me with this that'd be great!

I'm also interested in knowing how to change fonts, and text colors (although it's not necessary for this project).

Additionally, if anyone knows of a good tutorial on writing to files, that'd be greatly appreciated!
Last edited by SuperMetroid; Aug 9th, 2009 at 3:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,072
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 269
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Writing to MS Word.. Help?

 
0
  #2
Aug 9th, 2009
You'll need to actually write to your file handle txtfile by using txtfile.write( text ) .
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 17
Reputation: SuperMetroid is an unknown quantity at this point 
Solved Threads: 0
SuperMetroid's Avatar
SuperMetroid SuperMetroid is offline Offline
Newbie Poster

Re: Writing to MS Word.. Help?

 
0
  #3
Aug 9th, 2009
Okay, I changed it to the following, but when I run it and open the file, the document is still blank.

  1. txtfile = open('C:/Python31/SKUs.txt', 'w')
  2.  
  3. with txtfile:
  4. num = 817
  5. while num <= 820:
  6. x = "G" + str(num)
  7. txtfile.write(x)
  8. num += 1
  9.  
  10. txtfile.close()
Last edited by SuperMetroid; Aug 9th, 2009 at 4:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,072
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 269
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Writing to MS Word.. Help?

 
0
  #4
Aug 9th, 2009
Originally Posted by SuperMetroid View Post
Okay, I changed it to the following, but when I run it and open the file, the document is still blank.

  1. txtfile = open('C:/Python31/SKUs.txt', 'w')
  2.  
  3. with txtfile:
  4. num = 817
  5. while num <= 820:
  6. x = "G" + str(num)
  7. txtfile.write(x)
  8. num += 1
  9.  
  10. txtfile.close()
That code works fine for me. What platform are you on?
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 17
Reputation: SuperMetroid is an unknown quantity at this point 
Solved Threads: 0
SuperMetroid's Avatar
SuperMetroid SuperMetroid is offline Offline
Newbie Poster

Re: Writing to MS Word.. Help?

 
0
  #5
Aug 9th, 2009
That's rather unnerving. I'm using vanilla Python 3.1 on Windows XP.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,072
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 269
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Writing to MS Word.. Help?

 
0
  #6
Aug 9th, 2009
This shouldn't make a difference but the proper way to use the with statement is like so:
  1. with open('C:/Python31/SKUs.txt', 'w') as txtfile:
  2. num = 817
  3. while num <= 820:
  4. x = "G" + str(num)
  5. txtfile.write(x)
  6. num += 1
Notice I omit closing the file. That's because the with syntax automatically closes the file handle. AFter that loop you can test with txtfile.closed , which should return True because it's closed.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 17
Reputation: SuperMetroid is an unknown quantity at this point 
Solved Threads: 0
SuperMetroid's Avatar
SuperMetroid SuperMetroid is offline Offline
Newbie Poster

Re: Writing to MS Word.. Help?

 
0
  #7
Aug 9th, 2009
Nice! Okay thanks that's helpful.
Hmm.. I tried running the code on my mac (Python 3.1) and it yields no results either.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 330 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC