通过循环运行LibreOffice命令行脚本 [英] Run an LibreOffice Command-Line Script through a loop

查看:51
本文介绍了通过循环运行LibreOffice命令行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been able to successfully run the below code on an individual file, but I would like to find a way to have it run so that it loops through all the files shown in a directory. I believe that loops are possible within CMD.exe, but I am not having any success. Any help would be greatly appreciated.

"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\DBFCONVERT\57826001_JQSAMPLE.csv --outdir C:\Users\jdavidson\desktop\complete

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

If you have any ideas for how I might be able to achieve this it be a huge help. I am working to build this as part of a larger workflow and make it repeatable.

UPDATE

UPDATE

通过创建批处理文件并将其放置在我将用于转换前进的目录中,我能够遍历目录中的所有文件

I was able to iterate(Loop) through all of the files in my directory by creating a batch file and placing it within the directory that I am going to be using for converting moving forward

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\DBFCONVERT\57826001_JQSAMPLE.csv --outdir C:\Users\jdavidson\desktop\complete %%i

请注意,我使用了for循环来实现所需的功能.

Note that I used a for Loop to achieve what I wanted.

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/Libre Office ..就可以转换多个文档而无需循环.只需使用通配符,例如:

Option 1: You can actually convert multiple documents with just one execution of Open/Libre Office.. without a loop. Just use a wildcard, eg:

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

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

Option 2: I assume that Windows has a limit on the number of files you can pass in that way.. not sure what that limit is.. but if you exceed that limit, you can use this python program I wrote to do this just a few days ago.. as part of a project I am still working on. In my case I am converting .doc to .odt, and obviously my directories aren't the same as yours. But it would be quite easy for you to adjust this to your needs... One thing I wanted to do with mine is to check for the existence of the converted file and skip it if it exists.. so that if a few new files are added it can be run again to convert them without redoing them all...

应该将 docpath 设置为文件的位置,将 soffice 设置为Calc exe的路径.,该程序也将转换后的文件放入其中.并检查它们是否存在.另外,我走了寻找输入文件的路径.因此,如果由于某种原因您将文件放在不同的子目录中,它将找到它们并将转换结果放在同一目录中.

The docpath should be set to the location of your files, and soffice to the pathname of your Calc exe.. and if you leave things the way I have them, that's where the program puts the converted files too.. and checks for their existence. Also, I walk the path looking for input files.. so if for whatever reason you have your files in different subdirectories, it will find them and put the conversion in the same directory.

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 字符串设置为具有正确的转换设置.加上其他一些应该很容易的更改..但是,如果您需要帮助,请提出评论.

Just install Python27.. and set the convert string to have the correct settings for your conversion. Along with a few other changes that should be pretty easy.. but if you need help, ask in comments.

这篇关于通过循环运行LibreOffice命令行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆