Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Can I Import table from txt file into form letter using Python?

Reply
Thread Tools

Can I Import table from txt file into form letter using Python?

 
 
WorkerBee
Guest
Posts: n/a
 
      02-21-2013
Very new to python but diving in with both feet here.

I am trying to extend a python script to include automating a response formletter by including the results from a text file in tabular format into the letter.

I have a text file called “Cities” that I want to add to a paragraph indocument. (It can be a text document. I can copy it into a word document later)

The Cities.txt file looks like this:

"CITYNAME", Pop
"Master Chief", 4277647
"El Carro", 10017844
"Geat Stanton", 717804
"Eureka", 1973732
"Lands End",5329713
"Pembrook", 2751614
"White Park", 6854152
"Greenbriar", 3243175
"Lost Crutches", 1285470
"Storeybrook", 268888
"Burlington Heights City",10793
"Nyte", 11613743
"Dog Patch", 472314
"Miserable Lives", 2439334
"Lincoln Park Pirates", 6808633
"Son ofaFrancisco", 2295350
"Lois Lane Heights", 5178065

The first line is header info which will need to be renamed. The remaining lines need to be entered as two columns but doubled up across the page. Thefile will always be called cities but the data will change for each form letter. Thus the cities could be 100 records long, or no cities found depending on the search results. The table has to be 6.5 inches wide. Height willvary. Thus for this file the data will need to look like this:

"Below is a list of the following cities that are within your area of interest."

City Name Population City Name Population
Master Chief 4277647 Storeybrook 268888
El Carro 10017844 Burlington Heights City 10793
Geat Stanton 717804 Nyte 11613743
Eureka 1973732 Dog Patch 472314
Lands End 5329713 Miserable Lives 2439334
Pembrook 2751614 Lincoln Park Pirates 6808633
White Park 6854152 Son ofaFrancisco 2295350
Greenbriar 3243175 Lois Lane Heights 5178065
Lost Crutches 1285470

(Note: No data would say something like: "There are no cities within your area of interest." No table is inserted.)

Note that the city names sizes change so the column widths may need to change also. My current process is to import the text file into Excel. Rename and bold font headings and copy and paste second set of headings in columns 3 and 4. Cut and paste half the rows into new columns below the second set of headings. Copy and paste into word document. Auto fit table to window.

I actually have several text files. Some require 1, 2 or 3 SETS of columns.I figure if I can get coding for the above example, I can figure out how to change it for all other text files.

I am trying to automate as much as the process as possible. Using python, Ihave automated the processing required to get the text files, now I want to try to automate putting this data into a standardized word document. Basically I'm trying to automate a form letter that has a few modifications depending on the query results. Can anyone help me here, if not with the code,a site that would help me figure it out by myself?

BTW, I have NO control over the appearance of the form letter. A committee of silly bureaucrats argued endlessly over the format last year and thus its format is carved in stone. I just plug in the data like a good grunt worker bee, so please refrain from slamming me on the ridiculousness of multiple columns. Appearance is everything around here.

Thanks in advance.
 
Reply With Quote
 
 
 
 
accessnewbie@gmail.com
Guest
Posts: n/a
 
      02-21-2013
On Thursday, February 21, 2013 2:12:12 PM UTC-8, WorkerBee wrote:
> Very new to python but diving in with both feet here.
>
>
>
> I am trying to extend a python script to include automating a response form letter by including the results from a text file in tabular format into the letter.
>
>
>
> I have a text file called “Cities” that I want to add to a paragraph in document. (It can be a text document. I can copy it into a word documentlater)
>
>
>
> The Cities.txt file looks like this:
>
>
>
> "CITYNAME", Pop
>
> "Master Chief", 4277647
>
> "El Carro", 10017844
>
> "Geat Stanton", 717804
>
> "Eureka", 1973732
>
> "Lands End",5329713
>
> "Pembrook", 2751614
>
> "White Park", 6854152
>
> "Greenbriar", 3243175
>
> "Lost Crutches", 1285470
>
> "Storeybrook", 268888
>
> "Burlington Heights City",10793
>
> "Nyte", 11613743
>
> "Dog Patch", 472314
>
> "Miserable Lives", 2439334
>
> "Lincoln Park Pirates", 6808633
>
> "Son ofaFrancisco", 2295350
>
> "Lois Lane Heights", 5178065
>
>
>
> The first line is header info which will need to be renamed. The remaining lines need to be entered as two columns but doubled up across the page. The file will always be called cities but the data will change for each formletter. Thus the cities could be 100 records long, or no cities found depending on the search results. The table has to be 6.5 inches wide. Height will vary. Thus for this file the data will need to look like this:
>
>
>
> "Below is a list of the following cities that are within your area of interest."
>
>
>
> City Name Population City Name Population
>
> Master Chief 4277647 Storeybrook 268888
>
> El Carro 10017844 Burlington Heights City 10793
>
> Geat Stanton 717804 Nyte 11613743
>
> Eureka 1973732 Dog Patch 472314
>
> Lands End 5329713 Miserable Lives 2439334
>
> Pembrook 2751614 Lincoln Park Pirates 6808633
>
> White Park 6854152 Son ofaFrancisco 2295350
>
> Greenbriar 3243175 Lois Lane Heights 5178065
>
> Lost Crutches 1285470
>
>
>
> (Note: No data would say something like: "There are no cities within yourarea of interest." No table is inserted.)
>
>
>
> Note that the city names sizes change so the column widths may need to change also. My current process is to import the text file into Excel. Renameand bold font headings and copy and paste second set of headings in columns 3 and 4. Cut and paste half the rows into new columns below the second set of headings. Copy and paste into word document. Auto fit table to window.
>
>
>
> I actually have several text files. Some require 1, 2 or 3 SETS of columns. I figure if I can get coding for the above example, I can figure out howto change it for all other text files.
>
>
>
> I am trying to automate as much as the process as possible. Using python,I have automated the processing required to get the text files, now I wantto try to automate putting this data into a standardized word document. Basically I'm trying to automate a form letter that has a few modifications depending on the query results. Can anyone help me here, if not with the code, a site that would help me figure it out by myself?
>
>
>
> BTW, I have NO control over the appearance of the form letter. A committee of silly bureaucrats argued endlessly over the format last year and thus its format is carved in stone. I just plug in the data like a good grunt worker bee, so please refrain from slamming me on the ridiculousness of multiple columns. Appearance is everything around here.
>
>
>
> Thanks in advance.


Columns are supposed to line up. They lined up in my editor. Didn't see a preview/edit option so I had to post without using these features. Sorry.
 
Reply With Quote
 
 
 
 
accessnewbie@gmail.com
Guest
Posts: n/a
 
      02-21-2013
On Thursday, February 21, 2013 2:12:12 PM UTC-8, WorkerBee wrote:
> Very new to python but diving in with both feet here.
>
>
>
> I am trying to extend a python script to include automating a response form letter by including the results from a text file in tabular format into the letter.
>
>
>
> I have a text file called “Cities” that I want to add to a paragraph in document. (It can be a text document. I can copy it into a word documentlater)
>
>
>
> The Cities.txt file looks like this:
>
>
>
> "CITYNAME", Pop
>
> "Master Chief", 4277647
>
> "El Carro", 10017844
>
> "Geat Stanton", 717804
>
> "Eureka", 1973732
>
> "Lands End",5329713
>
> "Pembrook", 2751614
>
> "White Park", 6854152
>
> "Greenbriar", 3243175
>
> "Lost Crutches", 1285470
>
> "Storeybrook", 268888
>
> "Burlington Heights City",10793
>
> "Nyte", 11613743
>
> "Dog Patch", 472314
>
> "Miserable Lives", 2439334
>
> "Lincoln Park Pirates", 6808633
>
> "Son ofaFrancisco", 2295350
>
> "Lois Lane Heights", 5178065
>
>
>
> The first line is header info which will need to be renamed. The remaining lines need to be entered as two columns but doubled up across the page. The file will always be called cities but the data will change for each formletter. Thus the cities could be 100 records long, or no cities found depending on the search results. The table has to be 6.5 inches wide. Height will vary. Thus for this file the data will need to look like this:
>
>
>
> "Below is a list of the following cities that are within your area of interest."
>
>
>
> City Name Population City Name Population
>
> Master Chief 4277647 Storeybrook 268888
>
> El Carro 10017844 Burlington Heights City 10793
>
> Geat Stanton 717804 Nyte 11613743
>
> Eureka 1973732 Dog Patch 472314
>
> Lands End 5329713 Miserable Lives 2439334
>
> Pembrook 2751614 Lincoln Park Pirates 6808633
>
> White Park 6854152 Son ofaFrancisco 2295350
>
> Greenbriar 3243175 Lois Lane Heights 5178065
>
> Lost Crutches 1285470
>
>
>
> (Note: No data would say something like: "There are no cities within yourarea of interest." No table is inserted.)
>
>
>
> Note that the city names sizes change so the column widths may need to change also. My current process is to import the text file into Excel. Renameand bold font headings and copy and paste second set of headings in columns 3 and 4. Cut and paste half the rows into new columns below the second set of headings. Copy and paste into word document. Auto fit table to window.
>
>
>
> I actually have several text files. Some require 1, 2 or 3 SETS of columns. I figure if I can get coding for the above example, I can figure out howto change it for all other text files.
>
>
>
> I am trying to automate as much as the process as possible. Using python,I have automated the processing required to get the text files, now I wantto try to automate putting this data into a standardized word document. Basically I'm trying to automate a form letter that has a few modifications depending on the query results. Can anyone help me here, if not with the code, a site that would help me figure it out by myself?
>
>
>
> BTW, I have NO control over the appearance of the form letter. A committee of silly bureaucrats argued endlessly over the format last year and thus its format is carved in stone. I just plug in the data like a good grunt worker bee, so please refrain from slamming me on the ridiculousness of multiple columns. Appearance is everything around here.
>
>
>
> Thanks in advance.


OOPS. One more thing, I hope. Python version is 2.7. Locked into this version due to software constraints.
 
Reply With Quote
 
rob.marshall17@gmail.com
Guest
Posts: n/a
 
      02-22-2013
Using the data you supplied as is, you could try something like:

def print_cities(filename):
# cnt is used just to know if it's time for a newline
cnt = 0
cities = open(filename,'r').readlines()
for city in cities:
# Make sure we have a valid population
if re.match('\d+$',city.split(',')[-1].strip()):
cnt += 1
c, p = city.split(',')
print "{:25} {:15,}".format(c.strip().replace('"',''),int(p.str ip())),
if cnt % 2 == 0: print

Call it this way: print_cities('/path/to/Cities.txt'). The city names need to be <= 25 characters in length and the numbers <= 15 characters in length including commas but that would give you a number that is bigger than any city I know of You can adjust the column sizes as you wish and print the rest of the letter formatted around the cities.

Rob
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting how often the same word appears in a txt file...But my codeonly prints the last line entry in the txt file dgcosgrave@gmail.com Python 8 12-19-2012 06:29 PM
"rec.photo.digital.txt" Filter File Posted Online (for newsreadersthat can import a list of e-mail addresses to filter out) SMS 斯蒂文• 夏 Digital Photography 2 11-25-2007 11:00 AM
How can I import XML or CSV, or TXT files into an E-Mail Client Zygon Curry Computer Support 5 01-21-2007 03:51 PM
Opening a txt file to view ( i.e. readme.txt) Sameen C++ 2 08-29-2005 03:14 PM
import txt into database atse ASP General 4 10-15-2003 04:30 AM



Advertisments