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

Discussion in 'Python' started by WorkerBee, Feb 21, 2013.

  1. WorkerBee

    WorkerBee Guest

    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.
     
    WorkerBee, Feb 21, 2013
    #1
    1. Advertising

  2. WorkerBee

    Guest

    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.
     
    , Feb 21, 2013
    #2
    1. Advertising

  3. WorkerBee

    Guest

    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.
     
    , Feb 21, 2013
    #3
  4. WorkerBee

    Guest

    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
     
    , Feb 22, 2013
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vertigo

    big letter -> small letter

    vertigo, Jul 6, 2004, in forum: Python
    Replies:
    4
    Views:
    786
    Reinhold Birkenfeld
    Jul 6, 2004
  2. Tony Meyer

    RE: big letter -> small letter

    Tony Meyer, Jul 6, 2004, in forum: Python
    Replies:
    0
    Views:
    518
    Tony Meyer
    Jul 6, 2004
  3. Andrew McNamara

    Re: big letter -> small letter

    Andrew McNamara, Jul 6, 2004, in forum: Python
    Replies:
    2
    Views:
    768
    Scott David Daniels
    Jul 6, 2004
  4. Sameen
    Replies:
    2
    Views:
    467
    Victor Bazarov
    Aug 29, 2005
  5. atse

    import txt into database

    atse, Oct 14, 2003, in forum: ASP General
    Replies:
    4
    Views:
    283
Loading...

Share This Page