xoffice2txtwin (Gnu Make版)
gitで管理しているExcelやWordファイルをテキスト化して差分を見れるようにするためのアプリ(Windows用)

LibreOfficeでExcelなどのオフィス系ファイルをhtml化し、それをlynxでテキスト化します。

インストールしてパスを通す必要があるもの
pwsh
https://github.com/PowerShell/PowerShell/releases/

libreoffice
https://ja.libreoffice.org/

(pythonをインストールしている場合は、環境変数の定義の並び順を
LibreOfficeの環境変数より前にpythonの環境変数を設定しておく必要があります。
LibreOfficeのprogramフォルダーにLibreOffice用のpythonが存在するためです。)

lynx
https://lynx.invisible-island.net/
https://invisible-island.net/lynx/#installers
Stable releaseのslangにある
lynx-sl-setup.exeをダウンロードする。

nkf32
https://www.vector.co.jp/soft/win95/util/se295331.html

gnu make (equation版)
http://www.equation.com/servlet/equation.cmd?fa=make
make.exeをダウンロードする。

当アプリは、日本語名のExcel等のファイルの処理を想定しているため、sjisを使います。
しかし、Windows用gnu makeでダメ文字問題があるため、
Excel等のファイル名はダメ文字以外の文字を使用する必要があります。
Shift_JISのダメ文字
https://sites.google.com/site/fudist/Home/grep/sjis-damemoji-jp

下記から
xoffice2txtgmake.bat
xoffice2txtwin.mak
をコピペ、保存して(xoffice2txtwin.makは一応sjisで保存)Excel、Wordファイルがあるフォルダーへ置き、
最初に
make -f xoffice2txtwin.mak init_file
を実行しておき、
xoffice2txtgmake.bat
をコマンドラインからの実行か、ダブルクリックで処理開始します。

----------------------------------------------------------------------------------------------------
xoffice2txtgmake.bat
----------------------------------------------------------------------------------------------------
::This software includes the work that is distributed in the Apache License 2.0.
::https://www.apache.org/licenses/LICENSE-2.0

@echo off

::最初に
::make -f xoffice2txtwin.mak init_file
::を実行しておく

set text_file_names=text_file_names_win.txt

make -f xoffice2txtwin.mak TEXT_FILE_NAMES=%text_file_names% %*
make -f xoffice2txtwin.mak clean_tmp

----------------------------------------------------------------------------------------------------
xoffice2txtwin.mak
----------------------------------------------------------------------------------------------------
#This software includes the work that is distributed in the Apache License 2.0.
#https://www.apache.org/licenses/LICENSE-2.0

#xoffice2txtwin
#for gnu make

empty :=
space := $(empty) $(empty)
comma := ,

#SHELL := powershell.exe
SHELL := pwsh.exe
.SHELLFLAGS := -NoProfile -Command

TEXT_FILE_NAMES := text_file_names_win.txt

NKF := nkf32

XO_LANGUAGE := ja
LX_CHARSET := shift_jis
LX_WIDTH := 100
PS_ENCODING := default

tmp_dir := tmp_xoffice2txt_files

XOFFICE := soffice
XOFFICE_OPT := --nolockcheck --nologo --headless --norestore \
				--language=$(XO_LANGUAGE) --nofirststartwizard --convert-to

LYNX := lynx -display_charset=$(LX_CHARSET) -width=$(LX_WIDTH) -nolist -nomargins -dump

#日本語ファイル名を使う場合は下記ファイルはsjisで保存する必要がある(ダメ文字問題有ります)
files := $(file < $(TEXT_FILE_NAMES))

.PHONY: all
all: $(tmp_dir) $(files)

$(tmp_dir):
	if (!(test-path $(tmp_dir))) { mkdir $(tmp_dir) > $$nul }

.PHONY: init_file
init_file:
	#下記1行で改行エスケープの警告を抑制する
	$$ErrorActionPreference = "silentlycontinue"; \
	$$tmp = get-childitem . -name -rec -include *.xlsx, *.xls, *.docx, *.doc, *.ods, *.odt; \
	$$tmp = $$tmp -join " "; $$tmp = $$tmp -replace "\.(xlsx|xls|docx|doc|ods|odt)", ".txt"; \
	echo $$tmp | out-file -encoding $(PS_ENCODING) $(TEXT_FILE_NAMES); \
	$$shell_name = [System.IO.Path]::GetFileNameWithoutExtension("$(SHELL)"); \
	if ("$(XO_LANGUAGE)" -eq "ja" -and "$(OS)" -eq "Windows_NT" -and $$shell_name -eq "pwsh") { \
		$(NKF) -s --overwrite $(TEXT_FILE_NAMES); \
	} \
	echo "Success. output a $(TEXT_FILE_NAMES)"

.PHONY: clean_tmp
clean_tmp:
	if (test-path $(tmp_dir)) { rm $(tmp_dir) -recurse }

.PHONY: clean
clean:
	$$tmp = "$(subst $(space),"$(comma)",$(files))" | % { rm $$_ }

%.txt: %.html
	$$ErrorActionPreference = "silentlycontinue"; \
	if (test-path $<) { $(LYNX) $< | out-file -encoding $(PS_ENCODING) $@ } \
	if ((test-path $@) -and [System.IO.Path]::GetFileNameWithoutExtension("$(SHELL)") -eq "powershell") { \
		$(NKF) -w --overwrite $@; \
	} \
	if (test-path $<) { move -force $< $(tmp_dir) > $$nul }

%.html:
	$$ErrorActionPreference = "silentlycontinue"; \
	$$filter_names = [System.Collections.Hashtable]::new(); \
	$$filter_names.add(".xlsx", "StarCalc"); \
	$$filter_names.add(".xls", "StarCalc"); \
	$$filter_names.add(".ods", "StarCalc"); \
	$$filter_names.add(".docx", "StarWriter"); \
	$$filter_names.add(".doc", "StarWriter"); \
	$$filter_names.add(".odt", "StarWriter"); \
	\
	foreach ($$ext in $$filter_names.keys) { \
		$$file_path = "$*" + $$ext; \
		if (test-path $$file_path) { \
			$$filter_name = $$filter_names[$$ext]; \
			break; \
		} \
	} \
	\
	if (test-path $$file_path) { \
		$(XOFFICE) $(XOFFICE_OPT) "html:HTML ($$filter_name)" --outdir $(dir $@) $$file_path; \
	} \
	if (test-path $*_html_*.png) { move -force $*_html_*.png $(tmp_dir) > $$nul }

----------------------------------------------------------------------------------------------------
おまけ
touchwin.bat
----------------------------------------------------------------------------------------------------
::This software includes the work that is distributed in the Apache License 2.0.
::https://www.apache.org/licenses/LICENSE-2.0

@echo off

for %%A in (%*) do (
	if not exist %%A (
		type nul > %%A
	) else (
		copy /b %%A +,, > nul
	)
)

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です