Ignore:
Timestamp:
2019-04-19T20:34:03+12:00 (5 years ago)
Author:
davidb
Message:

Script upgraded to work with full set of PDFs provided by Heritage NZ

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/model-sites-dev/heritage-nz/collect/reports-2019/prepare/xls-to-csv.py

    r32820 r33018  
    2525        value = xlrd.error_text_from_code[value]
    2626    return value
     27
     28
     29letter_to_folder = {
     30  'a': "A - B",
     31  'b': "A - B",
     32  'c': "C - D",
     33  'd': "C - D",
     34  'e': "E - F",
     35  'f': "E - F",
     36  'g': "G - H",
     37  'h': "G - H",
     38  'i': "I - J",
     39  'j': "I - J",
     40  'k': "K - L",
     41  'l': "K - L",
     42  'm': "M - N",
     43  'n': "M - N",
     44  'o': "O - P",
     45  'p': "O - P",
     46  'q': "Q - R",
     47  'r': "Q - R",
     48  's': "S - T",
     49  't': "S - T",
     50  'u': "U - V",
     51  'v': "U - V",
     52  'w': "W - Z",
     53  'x': "W - Z",
     54  'y': "W - Z",
     55  'z': "W - Z"
     56}
     57
     58def id_to_relative_pdf_file(id):
     59    first_char = id[0:1]
     60    first_char_lower = first_char.lower()
     61
     62    file_tail = letter_to_folder[first_char_lower]
     63    file_root = "Reports " + file_tail
     64
     65    pdf_file = os.path.join("Digital-Library-PDFs",file_root,id+".pdf")
     66
     67    return pdf_file
    2768
    2869
     
    103144                if col_j == 0:
    104145                    # Check to see if companion PDF file present
    105                     pdf_filename = os.path.join(excel_dirname,"pdfs",formatted_cell+".pdf")
     146                    # pdf_filename = os.path.join(excel_dirname,"pdfs",formatted_cell+".pdf")
     147                    id = formatted_cell.replace(" ","")
     148
     149                    pdf_file = id_to_relative_pdf_file(id)
     150                    pdf_file_root, pdf_ext = os.path.splitext(pdf_file)
     151                    PDF_file = pdf_file_root + ".PDF"
     152
     153                    pdf_filename = os.path.join(excel_dirname,pdf_file)
     154                    PDF_filename = os.path.join(excel_dirname,PDF_file)
     155
    106156                    if os.path.exists(pdf_filename):
    107157                        found_pdf = True
    108                         pdf_file = os.path.join("pdfs",formatted_cell+".pdf")
    109158                        formatted_utf8_row.insert(0, pdf_file)
    110                                  
     159                    elif os.path.exists(PDF_filename):
     160                        found_pdf = True
     161                        formatted_utf8_row.insert(0, PDF_file)
     162                    else:
     163                        print "Unbound id: '" + id + "'"
     164
     165                if isinstance(formatted_cell, basestring):
     166                    # Remove any trailing whitespace. 
     167                    # Newline at end particular harmful for a entry in the CSV file
     168                    formatted_cell = formatted_cell.rstrip();
     169
    111170                formatted_cell_utf8 = unicode(formatted_cell).encode("utf-8")
    112171                formatted_utf8_row.append(formatted_cell_utf8)
Note: See TracChangeset for help on using the changeset viewer.