运行 一个循环的 LibreOffice 命令行脚本

Run an LibreOffice Command-Line Script through a loop

我已经能够在单个文件上成功地 运行 下面的代码,但我想找到一种方法来使用它 运行 以便它循环遍历中所示的所有文件一个目录。我相信在 CMD.exe 内循环是可能的,但我没有取得任何成功。任何帮助将不胜感激。

"C:\Program Files (x86)\LibreOffice 5\program\scalc.exe" --convert-to DBF --infilter="csv:44,34,76,1,1/2/2/2/3/2/4/2/5/2/6/2/7/2/8/2/9/2/10/2/11/2/12/2/13/2/14/2/15/2/16/2/17/2/18/2/19/2/20/2/21/2/22/2/23/2/24/2/25/2/26/2/27/2/28/2/29/2/30/2/31/2/32/2/33/2/34/2/35/2/36/2/37/2/38/2/39/2/40/2/41/2/42/2/43/2/44/2/45/2/46/2/47/2/48/2/49/2/50/2/51/2/52/2/53/2/54/2/55/2/56/2/57/2/58/2/59/2/60/2/61/2/62/2/63/2/64/2/65/2/66/2/67/2/68/2/69/2/70/2/71/2/72/2/73/2/74/2/75/2/76/2/77/2/78/2/79/2/80/2/81/2/82/2/83/2/84/2/85/2/86/2/87/2/88/2/89/2/90/2/91/2/92/2/93/2/94/2/95/2/96/2/97/2/98/2/99/2/100/2/101/2/102/2/103/2/104/2/105/2/106/2/107/2/108/2/109/2/110/2/111/2/112/2/113/2/114/2/115/2/116/2/117/2/118/2/119/2/120/2/121/2/122/2/123/2/124/2/125/2/126/2/127/2/128/2/129/2/130/2/131/2/132/2/133/2/134/2/135/2/136/2,0,false,false" C:\Users\jdavidson\Desktop\DBFCONVERT826001_JQSAMPLE.csv --outdir C:\Users\jdavidson\desktop\complete

如果您对我如何实现这一目标有任何想法,那将是一个巨大的帮助。我正在努力将其构建为更大工作流程的一部分并使其可重复。

更新

我能够通过创建一个批处理文件并将其放置在我将用于向前转换的目录中来迭代(循环)目录中的所有文件

for /r %%i in (*.csv) do "C:\Program Files (x86)\LibreOffice 5\program\scalc.exe" --convert-to DBF --infilter="csv:44,34,76,1,1/2/2/2/3/2/4/2/5/2/6/2/7/2/8/2/9/2/10/2/11/2/12/2/13/2/14/2/15/2/16/2/17/2/18/2/19/2/20/2/21/2/22/2/23/2/24/2/25/2/26/2/27/2/28/2/29/2/30/2/31/2/32/2/33/2/34/2/35/2/36/2/37/2/38/2/39/2/40/2/41/2/42/2/43/2/44/2/45/2/46/2/47/2/48/2/49/2/50/2/51/2/52/2/53/2/54/2/55/2/56/2/57/2/58/2/59/2/60/2/61/2/62/2/63/2/64/2/65/2/66/2/67/2/68/2/69/2/70/2/71/2/72/2/73/2/74/2/75/2/76/2/77/2/78/2/79/2/80/2/81/2/82/2/83/2/84/2/85/2/86/2/87/2/88/2/89/2/90/2/91/2/92/2/93/2/94/2/95/2/96/2/97/2/98/2/99/2/100/2/101/2/102/2/103/2/104/2/105/2/106/2/107/2/108/2/109/2/110/2/111/2/112/2/113/2/114/2/115/2/116/2/117/2/118/2/119/2/120/2/121/2/122/2/123/2/124/2/125/2/126/2/127/2/128/2/129/2/130/2/131/2/132/2/133/2/134/2/135/2/136/2,0,false,false" C:\Users\jdavidson\Desktop\DBFCONVERT826001_JQSAMPLE.csv --outdir C:\Users\jdavidson\desktop\complete %%i

请注意,我使用了一个 for 循环来实现我想要的。

for /r %%i in ('list directory if command not being run in specific directory already; you can also but * for all files in directory or *.ext for the extension of only certain files) do 'add in command %%i

选项 1:实际上,您只需执行一次 Open/LibreOffice.. 就可以转换多个文档,无需循环。只需使用通配符,例如:

    "C:\Program Files (x86)\LibreOffice 5\program\scalc.exe" --convert-to DBF mydir\*.csv

选项 2:我假设 Windows 对您可以通过这种方式传递的文件数量有限制。不确定该限制是多少。但是如果您超过该限制,您可以使用这个 python 程序是我几天前写的。作为我仍在进行的项目的一部分。就我而言,我正在将 .doc 转换为 .odt,显然我的目录与您的不同。但是你可以很容易地根据你的需要调整它......我想做的一件事是检查转换后的文件是否存在并跳过它如果存在..这样如果有一些新文件添加后可以再次 运行 转换它们而无需全部重做...

docpath 应设置为文件的位置,soffice 应设置为 Calc exe 的路径名。程序也将转换后的文件放入……并检查它们是否存在。另外,我沿着路径寻找输入文件..所以如果出于某种原因你的文件在不同的子目录中,它会找到它们并将转换放在同一目录中。

import os, sys, re, subprocess

docpath = 'doc'
soffice = 'C:\Program Files\LibreOffice 5\program\soffice.exe'

convert = '"%s" --headless --convert-to odt --outdir "%s" "%s"'

def plog(fmt = '', *args):
    sys.stdout.write(fmt % args)
    sys.stdout.flush()

def log(fmt = '', *args):
    plog((fmt + '\n'), *args)

def convert():
    odtfiles = []
    for subdir, dirs, files in os.walk(docpath):
        for file in files:
            if not file.endswith('.doc'):
                continue
            docfile = os.path.join(subdir, file)
            odtfile = re.sub(r"\.doc$", ".odt", docfile)
            plog("%s -> %s: " % (docfile, odtfile))
            if not os.path.isfile(odtfile):
                 plog('Converting.. ')
                 subprocess.check_output(convert % (soffice, docpath, docfile), shell=True)
            log('OK.')
            odtfiles.append(odtfile)
        return odtfiles

odtfiles = convert()

只需安装 Python27.. 并设置 convert 字符串以获得正确的转换设置。连同其他一些应该很容易的更改..但如果您需要帮助,请在评论中提问。