Pythonでマクロ:住所録の処理


#coding: utf-8
import re
import uno
import screen_io as ui
import unohelper
from com.sun.star.awt import XKeyListener
#
#
titleNames = ['氏名', '郵便番号', '住所', '電話番号']
defNames ={'氏名':'nameColumn', '郵便番号':'zipcodeColumn',\
'住所':'addressColumn', '電話番号':'telephonColumn'}
processOrder=['郵便番号','電話番号','住所','氏名']
#
#
#新しいクラスを定義
class MyKeyListener(unohelper.Base, XKeyListener):
  def __init__(self, sheet, textf1):
    self.sheet = sheet
    self.textf1 = textf1
    self.row = 0
    #タイトルをシートに書く
    for i, titleName in enumerate(titleNames):
      self.sheet.getCellByPosition(i, self.row).String=titleName
  #メソッド
  def keyPressed( self,  event ):
    k = event.KeyCode
    c = event.KeyChar.value 
    mods = event.Modifiers
    # mods are additive
    # 0 - None
    # 1 - Shift
    # 2 - Ctrl
    # 4 - Alt
    # 8 - Super_R
    print(k, c, mods)
    if k == 1282:  #キー
      #表への書き出し
      self.row+=1
      print(self.row)
      textd = self.textf1.Text
      columnd_raw=textd.split("\n")
      #空行の除去
      columnd = []
      for item in columnd_raw:
        if item != '':
          columnd.append(item)
      #処理順位に従い処理
      for pOrder in processOrder:
        if len(columnd) == 0:
           ui.Print("未特定データなし")
           break
        #辞書からプロセス名を得る
        pName = defNames[pOrder]
        m= eval(pName)(columnd)
        if bool(m):
          columnd.remove(m)
        else:
          msg = pOrder+':該当行なし'
          ui.Print(msg)
          m=' '
        #print(pOrder, pName,  m)
        #シートへの書き込み
        self.sheet.getCellByPosition(titleNames.index(pOrder),self.row).String=m
      #テキストボックスを消去
      self.textf1.Text = ''

#マクロの開始
def createDialog(*args):
  ctx = XSCRIPTCONTEXT.getComponentContext()
  smgr = ctx.getServiceManager()
  dp = smgr.createInstanceWithContext("com.sun.star.awt.DialogProvider", ctx)
  dialog = dp.createDialog("vnd.sun.star.script:Standard.Dialog5?location=application")
#表
  doc = XSCRIPTCONTEXT.getDocument()
  sheet = doc.Sheets[0]
#コントロールの登録
  textf1 =  dialog.getControl("TextField1")
#エヴェント監視(Enterキーを検出)
  textf1_listener = MyKeyListener(sheet, textf1)
  textf1.addKeyListener(textf1_listener)
  dialog.execute()
  dialog.dispose()

#郵便番号処理
def zipcodeColumn(msgs):
  for i, msg in enumerate(msgs):
    m1 = re.search(r'^〒', msg)
    m2 = re.search(r'^\d{3}\-\d{4}$', msg)
    m3 = re.search(r'^\d{7}$', msg)
    if bool(m1) or bool(m2) or bool(m3):
      return msg
  return None

#電話番号処理
def telephonColumn(msgs):
  for i, msg in enumerate(msgs):
    m1 = re.search(r'^電話|Tel|TEL', msg)
    #print(i, m1)
    m2 = re.search(r'[^0-9-()+]', msg)
    #print(i, m2)
    if bool(m1) or not bool(m2):
      return msg
  return None

#住所処理    
def addressColumn(msgs):
  for i, msg in enumerate(msgs):
    ms = []
    m1 = re.search(r'都|道|府|県', msg)
    ms.append(m1)
    m2 = re.search(r'郡|市', msg)
    ms.append(m2)
    m3 = re.search(r'区|町|村|大字', msg)
    ms.append(m3)
    m4 = re.search(r'丁|番|号|字', msg)
    count = 0
    for m in ms:
      if bool(m):
        count+=1
    if count >= 2:
      return msg
  return None 

#氏名処理
def nameColumn(msgs):
  return msgs[0]