1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

1
Last updated at Posted at 2022-02-26

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

インストールしてパスを通す必要があるもの
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

jom(nmake互換make)
https://wiki.qt.io/Jom
http://download.qt.io/official_releases/jom/jom.zip
をダウンロードする

nmakeを使う場合はVisualStudio等に同梱されているものを使う

当アプリは、日本語名のExcel等のファイルを扱う場合、
読み込みファイル(text_file_names.txt)はUTF-8BOMである必要があります。

jomの場合
下記から
xoffice2txt.bat
xoffice2txt.jom
をコピペ、保存して(xoffice2txt.jomはsjisでもutf-8でもよい)
Excel、Wordファイルがあるフォルダーへ置き

jom -lf xoffice2txt.jom init_file

を実行してから
xoffice2txt.bat
をコマンドラインからの実行か、ダブルクリックで処理開始します。

nmakeの場合
下記から
xoffice2txtnmake.bat
xoffice2txt.jom
をコピペ、保存して(xoffice2txt.jomはsjisでもutf-8でもよい)
Excel、Wordファイルがあるフォルダーへ置き

nmake /nologo /f xoffice2txt.jom init_file

を実行してから
xoffice2txtnmake.bat
をコマンドラインからの実行か、ダブルクリックで処理開始します。

############################################################
jom版
xoffice2txt.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

::最初に
::jom -lf xoffice2txt.jom init_file
::を実行しておく

set text_file_names=text_file_names.txt

jom -lf xoffice2txt.jom -j 1 TEXT_FILE_NAMES=%text_file_names% %*
jom -lf xoffice2txt.jom clean_tmp

############################################################
nmake版
xoffice2txtnmake.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

::最初に
::nmake /nologo /f  xoffice2txt.jom init_file
::を実行しておく

set text_file_names=text_file_names.txt

nmake /nologo /f xoffice2txt.jom TEXT_FILE_NAMES=%text_file_names% %*
nmake /nologo /f xoffice2txt.jom clean_tmp

jom版
xoffice2txt.jom

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

#xoffice2txt
#for jom (nmake)

#当ファイルをjomで実行時はオプションを -j 1で実行する必要があります

.SUFFIXES:
.SUFFIXES: .txt .xlsx .xls .ods .docx .doc .odt

TEXT_FILE_NAMES = text_file_names.txt

NKF = nkf32

LO_LANGUAGE = ja
LX_CHARSET = shift_jis
LX_WIDTH = 100

tmp_dir = tmp_xoffice2txt_files

LIBREOFFICE = soffice --nolockcheck --nologo --headless --norestore\
				--language=$(LO_LANGUAGE) --nofirststartwizard --convert-to

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

!IF EXIST ($(TEXT_FILE_NAMES))
!include $(TEXT_FILE_NAMES)
!ENDIF

all: $(tmp_dir) $(FILES)

$(tmp_dir):
	if not exist $(tmp_dir) mkdir $(tmp_dir) > nul

init_file:
	call <<createfile.bat
@echo off
setlocal enableDelayedExpansion
(echo FILES =\) > $(TEXT_FILE_NAMES)
for /r %CD% %%A in (*.xls? *.ods *.doc? *.odt) do (
	set file_path=%%~dpA
	(echo !file_path:%CD%\=!%%~nA.txt\) >> $(TEXT_FILE_NAMES)
)
set make_name=$(MAKE)
if "%make_name:~-7%" == "jom.exe" (
	if "$(LO_LANGUAGE)" == "ja" ($(NKF) --overwrite --oc=UTF-8-BOM $(TEXT_FILE_NAMES))
)
endlocal
<<

clean_tmp:
	rmdir /s /q $(tmp_dir)

clean:
	del $(FILES)

#MSOffice

.xlsx.txt:
	$(LIBREOFFICE) "html:HTML (StarCalc)" --outdir $(@D) $<
	$(LYNX) $*.html > $@
	if "$(LO_LANGUAGE)" == "ja" ($(NKF) -w -Lw --overwrite $@)
	if exist $*.html move /y $*.html $(tmp_dir) > nul
	if exist $*_html_*.png move /y $*_html_*.png $(tmp_dir) > nul

#abcというフォルダー配下にファイルが存在する場合には手作業で下記のように処理ブロックを複製し、追記する
{abc}.xlsx{abc}.txt:
	$(LIBREOFFICE) "html:HTML (StarCalc)" --outdir $(@D) $<
	$(LYNX) $*.html > $@
	if "$(LO_LANGUAGE)" == "ja" ($(NKF) -w -Lw --overwrite $@)
	if exist $*.html move /y $*.html $(tmp_dir) > nul
	if exist $*_html_*.png move /y $*_html_*.png $(tmp_dir) > nul

.xls.txt:
	$(LIBREOFFICE) "html:HTML (StarCalc)" --outdir $(@D) $<
	$(LYNX) $*.html > $@
	if "$(LO_LANGUAGE)" == "ja" ($(NKF) -w -Lw --overwrite $@)
	if exist $*.html move /y $*.html $(tmp_dir) > nul
	if exist $*_html_*.png move /y $*_html_*.png $(tmp_dir) > nul

.docx.txt:
	$(LIBREOFFICE) "html:HTML (StarWriter)" --outdir $(@D) $<
	$(LYNX) $*.html > $@
	if "$(LO_LANGUAGE)" == "ja" ($(NKF) -w -Lw --overwrite $@)
	if exist $*.html move /y $*.html $(tmp_dir) > nul
	if exist $*_html_*.png move /y $*_html_*.png $(tmp_dir) > nul

.doc.txt:
	$(LIBREOFFICE) "html:HTML (StarWriter)" --outdir $(@D) $<
	$(LYNX) $*.html > $@
	if "$(LO_LANGUAGE)" == "ja" ($(NKF) -w -Lw --overwrite $@)
	if exist $*.html move /y $*.html $(tmp_dir) > nul
	if exist $*_html_*.png move /y $*_html_*.png $(tmp_dir) > nul

#LibreOffice

.ods.txt:
	$(LIBREOFFICE) "html:HTML (StarCalc)" --outdir $(@D) $<
	$(LYNX) $*.html > $@
	if "$(LO_LANGUAGE)" == "ja" ($(NKF) -w -Lw --overwrite $@)
	if exist $*.html move /y $*.html $(tmp_dir) > nul
	if exist $*_html_*.png move /y $*_html_*.png $(tmp_dir) > nul

.odt.txt:
	$(LIBREOFFICE) "html:HTML (StarWriter)" --outdir $(@D) $<
	$(LYNX) $*.html > $@
	if "$(LO_LANGUAGE)" == "ja" ($(NKF) -w -Lw --overwrite $@)
	if exist $*.html move /y $*.html $(tmp_dir) > nul
	if exist $*_html_*.png move /y $*_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
	)
)

元記事
https://egg-sandwich.com/blog/2022/02/05/xoffice2txt-jom%e3%80%81nmake%e7%89%88-git%e3%81%a7%e7%ae%a1%e7%90%86%e3%81%97%e3%81%a6%e3%81%84%e3%82%8bexcel%e3%82%84word%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e3%83%86%e3%82%ad%e3%82%b9/

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?