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

W

WorkerBee

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.
 
A

accessnewbie

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.
 
A

accessnewbie

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.
 
R

rob.marshall17

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.strip())),
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top