PythonでテキストファイルをWord形式に変換2
前回Pythonにpython-docxというライブラリをインストールしました。このライブラリを使ってワードデータに変換してみます。
手順
1.テキストデータを読み込む
2.テキストデータを置換
3.テキストデータをワード形式に保存
こんな感じになるかなと思います。
コードを書いていく
まずはテキストの読み込みをします
1.テキストデータを読み込む
import re
from docx import Document
from docx.shared import RGBColor
from docx.shared import Inches
from docx.shared import Pt
'テキストの読み込み'
path = 'sample_python_txt.txt'
with open(path) as open_file :
text = open_file.read()
print (text)
実行すると以下のようにでます
整理番号:322636940
日付:令和2年4月15日
平成:30年5月 2日
提出人:544506015206
参考人:青しそ大葉様
提出日:昭和16年2月 29日
提出番号:PYT/MQL4827/793784
通知番号:通知256488
2.テキストデータを置換
置換はreplaceでできます。【】を付けたいのでやっていきます。
import re
from docx import Document
from docx.shared import RGBColor
from docx.shared import Inches
from docx.shared import Pt
'テキストの読み込み'
path = 'sample_python_txt.txt'
with open(path) as open_file :
text = open_file.read()
text = text.replace('整理番号:', '【整理番号】')
text = text.replace('日付:', '【日付】')
text = text.replace('平成:', '【平成】')
text = text.replace('提出人:', '【提出人】')
text = text.replace('参考人:', '【参考人】')
text = text.replace('提出日:', '【提出日】')
text = text.replace('提出番号:', '【提出番号】')
text = text.replace('通知番号:', '【通知番号】')
print (text)
このように置換をします。実行すると以下のようになります。
【整理番号】322636940
【日付】令和2年4月15日
【平成】30年5月 2日
【提出人】544506015206
【参考人】青しそ大葉様
【提出日】昭和16年2月 29日
【提出番号】PYT/MQL4827/793784
【通知番号】通知256488
スペースの調整が必要になります。面倒なのでスペースを直にかいてしまうのが楽かもしれません。今回はタブで調整してみます
import re
from docx import Document
from docx.shared import RGBColor
from docx.shared import Inches
from docx.shared import Pt
'テキストの読み込み'
path = 'sample_python_txt.txt'
with open(path) as open_file :
text = open_file.read()
text = text.replace('整理番号:', '【整理番号】 ')
text = text.replace('日付:', '【日付】 ')
text = text.replace('平成:', '【平成】 ')
text = text.replace('提出人:', '【提出人】 ')
text = text.replace('参考人:', '【参考人】 ')
text = text.replace('提出日:', '【提出日】 ')
text = text.replace('提出番号:', '【提出番号】 ')
text = text.replace('通知番号:', '【通知番号】 ')
print (text)
タブを1つと2つで調整しました。実行すると調整された状態ででます。
3.テキストデータをワード形式に保存
置換が終わったので最後にワード形式に保存をします。
※テキストはUTF-8でやっています。python-docxの使い方は詳しくは公式サイトをみてください。
document = Document() #書き込み
document.add_paragraph(text) #保存
document.save('word_demo.docx')
このコードを追加します。書き込みのとこでワードにテキストデータを挿入うしています。最後の保存のとこでワードデータとして保存しています。
実際に実行すると添付のファイルが作成されます。Word で開く事ができます。
最終的なコード
最終的なコードはこのようになります。
import re
from docx import Document
from docx.shared import RGBColor
from docx.shared import Inches
from docx.shared import Pt
'テキストの読み込み'
path = 'sample_python_txt.txt'
with open(path) as open_file :
text = open_file.read()
text = text.replace('整理番号:', '【整理番号】 ')
text = text.replace('日付:', '【日付】 ')
text = text.replace('平成:', '【平成】 ')
text = text.replace('提出人:', '【提出人】 ')
text = text.replace('参考人:', '【参考人】 ')
text = text.replace('提出日:', '【提出日】 ')
text = text.replace('提出番号:', '【提出番号】 ')
text = text.replace('通知番号:', '【通知番号】 ')
print (text)
document = Document() #書き込み
document.add_paragraph(text)
#保存
document.save('word_demo.docx')
このファイルはUTF-8なのか?Shift-JISなのか?
Word で利用可能なエンコード標準を調べるのページを見ると、Shift_jisの場合は明朝系で開かれるみたいです。デモは明朝なのでもしかしたらShift_jisで吐き出されているかも知れません。
現時点では不明、わからないというのが答えになります。もっと勉強をして、ファイルの文字コードを調べるようにしたみたいと思います。
お役に立ちましたか?
投げ銭をいただけるともっと頑張れます!
・note
・仮想通貨(bitflyer)
・キャッシュ
のどれでも構いません
ビットコインアドレスは以下になります
3LHnADwZwUbic2L45EnVJEykiG6KfbqrwS
この記事が気に入ったらサポートをしてみませんか?