Archive for: 𔃳月 2016’

Windows7でPython3.5環境で cx_Oracleを使う

2016年3月18日 Posted by PURGE

pythonでOracleへ接続したい。

import cx_Oracle
import os
os.environ["NLS_LANG"] = "Japanese_Japan.AL32UTF8"

tns = cx_Oracle.makedsn("192.168.0.1",1521,"ORASID")
connection = cx_Oracle.connect("user","pass",tns)
#connection = cx_Oracle.connect("user","pass","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.1) (PORT=1521))(CONNECT_DATA=(SID=ORASID)))")

cursor = connection.cursor()
agent_cd = '7'
sql = """
    select agent_cd, agent_name
      from agents
     where agent_cd = :agent_cd
    """
cursor.execute(sql, agent_cd)
rows = cursor.fetchall()
for agent in rows:
    print(agent[0], agent[1])


Windows7でPython3.5環境で python-docxを使う

2016年3月17日 Posted by PURGE

python-docxを使用しようと思ったが、ライブラリがなくエラー。

https://python-docx.readthedocs.org/en/latest/

コンパイラがないと怒られたので、Visual Studio15をダウンロードしてインストール。

しかし、lxml が無いとエラーで怒られる。
そこで単独インストールしたが、どうもうまく行かない。

easy_install lxml
include ファイルを開けません。'libxml/xpath.h':No such file or directory

そこで、下記サイトへ行って直接ファイルをダウンロード。
http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml

直接インストール。

pip3 install lxml-3.5.0-cp35-none-win32.whl

動作した。

#from docx import Document
from docxtpl import DocxTemplate

document = DocxTemplate('docx_tpl.docx')
context = {'title' : "タイトル", 'greeting' : "こんにちは。"}
document.render(context)
document.save('out_docx.docx')

テンプレートとなるwordファイルには、{{ title }} {{ greeting }} のタグを埋め込めばよい。

Windowsのdirコマンドでファイル名の一覧取得

2016年3月17日 Posted by PURGE

仕事上、ファイル一覧を取得して、Excel等に張り付けたい場合がある。
このコマンドを実行して、張り付ければOK。

dir /b | clip