0
点赞
收藏
分享

微信扫一扫

印刷把刀线长度添加到文件名里面

河南妞 2022-04-06 阅读 52
javapython

分2步走

1. 把pdf文件转svg

package cn.net.haotuo.htutils.utils;

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 把pdf转svg
 */
public class PdfToSvg {
    public static void main(String[] args) {
        String pdfPath ="C:\\Users\\Administrator\\Desktop\\pdf";
        String svgPath = "C:\\Users\\Administrator\\Desktop\\svg";
        pdf2svg(pdfPath, svgPath);
    }

    private static void pdf2svg(String pdfPath, String svgPath) {
        File[] files = new File(pdfPath).listFiles();
        for(File file:files){
            //转为单个svg
            if(file.getName().toLowerCase().indexOf("pdf")==-1){
                continue;
            }
            PdfDocument pdf = new PdfDocument(file.toString());
            String parent =svgPath;
            String parent2 =parent+"//temp";
            new File(parent2).mkdirs();
            String name = file.getName();
            name = name.substring(0,name.lastIndexOf("."));
            pdf.saveToFile(parent2+"/"+name+".svg", FileFormat.SVG);
            List<String> listSvg = ReadAndWriteFile.readtFile(parent2 + "/" + name + ".svg", "utf-8");
            List<String> listResult = new ArrayList<>();
            listResult.add("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            listResult.add("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">");
            for(String str:listSvg){
                if(str.indexOf("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"")!=-1){
                    listResult.add(str);
                }
            }
            listResult.add("<defs>");
            listResult.add("</defs>");
            listResult.add("<g transform=\"matrix(1.333333 0 0 1.333333 0 0)\">");
            for(String str:listSvg){
                if(str.indexOf("<path stroke=\"#")!=-1){
                    listResult.add(str);
                }
            }
            listResult.add("</g>");
            listResult.add("</svg>");
            StringBuffer stringBuffer = new StringBuffer();
            for(String str:listResult){
                stringBuffer.append(str+"\n");
            }
            ReadAndWriteFile.writeInFile(parent+"/"+name+".svg",stringBuffer.toString(),"utf-8");
        }
    }
}


用到的工具类

package cn.net.haotuo.htutils.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ReadAndWriteFile {
    /**
     *
     * @param path
     * @param charsetName utf-8 gbk
     * @return
     */
    public static List<String> readtFile(String path,String charsetName) {
        if(charsetName==null||charsetName.equals("")){
            charsetName = "utf-8";
        }
        List<String> list = new ArrayList<>();
        InputStreamReader read = null;// 考虑到编码格式
        try {
            read = new InputStreamReader(new FileInputStream(path), charsetName);
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = bufferedReader.readLine();
            while ( lineTxt!=null){
                list.add(lineTxt);
                lineTxt = bufferedReader.readLine();
            }
            read.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     *
     * @param path
     * @param content utf-8 gbk
     * @param charsetName
     */
    public static void writeInFile(String path, String content,String charsetName) {
        if(charsetName==null||charsetName.equals("")){
            charsetName = "utf-8";
        }
        BufferedWriter writer = null;
        StringBuilder outputString = new StringBuilder();
        try {
            outputString.append(content );
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),charsetName));
            writer.write(outputString.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
    }
}

2. 把svg的长度取出来,顺便把长度加在文件名上面

from svgpathtools import svg2paths
# pip --default-timeout=100 install svgpathtools -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
import os

# 获取目录下面文件夹
def get_files(path):
    _list = []
    for filepath,dirnames,filenames in os.walk(path):
        for filename in filenames:
            _list.append((filepath,filename))
    return _list

# 获取svg的长度
def get_length(path):
    paths, attributes = svg2paths(path)
    sum = 0
    for p in paths:
        mypath = p
        f  = 1.333333333/(72/25.4)
        sum +=  mypath.length()*f
    return sum

# 把刀模长度增加在文件名上面
def add_len_to_name(f):
    for filepath,filename in get_files(f):
        f2  = os.path.join(filepath,filename)
        len = int(get_length(f2)/1.333333333)
        os.rename(f2,os.path.join(filepath,str(len)+'-'+filename))

if __name__ == '__main__':
    add_len_to_name(r'C:\Users\Administrator\Desktop\svg')

3. 最终效果如下

在这里插入图片描述

举报

相关推荐

0 条评论