943,553 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 578
  • Python RSS
Aug 9th, 2009
0

Writing to MS Word.. Help?

Expand Post »
Why don't either of these implementations actually write to the file?

Code v1:
Python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  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):
Python Syntax (Toggle Plain Text)
  1.  
  2. -----------------------------
  3. | G817 G818 G819 |
  4. | | <-- page 1
  5. | |
  6. | |
  7. |___________________________|

Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SuperMetroid is offline Offline
17 posts
since Aug 2009
Aug 9th, 2009
0

Re: Writing to MS Word.. Help?

You'll need to actually write to your file handle txtfile by using txtfile.write( text ) .
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Aug 9th, 2009
0

Re: Writing to MS Word.. Help?

Okay, I changed it to the following, but when I run it and open the file, the document is still blank.

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SuperMetroid is offline Offline
17 posts
since Aug 2009
Aug 9th, 2009
0

Re: Writing to MS Word.. Help?

Okay, I changed it to the following, but when I run it and open the file, the document is still blank.

Python Syntax (Toggle Plain Text)
  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?
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Aug 9th, 2009
0

Re: Writing to MS Word.. Help?

That's rather unnerving. I'm using vanilla Python 3.1 on Windows XP.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SuperMetroid is offline Offline
17 posts
since Aug 2009
Aug 9th, 2009
0

Re: Writing to MS Word.. Help?

This shouldn't make a difference but the proper way to use the with statement is like so:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Aug 9th, 2009
0

Re: Writing to MS Word.. Help?

Nice! Okay thanks that's helpful.
Hmm.. I tried running the code on my mac (Python 3.1) and it yields no results either.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SuperMetroid is offline Offline
17 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: finding/returning lowest/highest int in lists, in a tuple
Next Thread in Python Forum Timeline: Using a Library e.g. chilkat





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC