【问题标题】:How to create svg file from data in csv file to create nameplates in a bash script如何从 csv 文件中的数据创建 svg 文件以在 bash 脚本中创建铭牌
【发布时间】:2019-11-17 06:35:19
【问题描述】:

我有一个 csv 文件,其中包含几个人的姓名、学位和其他数据,还有一个模板 svg 文件,它显示了输出的外观。现在我应该编写一个 bash 脚本,该脚本使用 csv 文件中的数据创建一个新的 svg 文件,该文件可以转换为 pdf 并打印出来以便稍后剪掉铭牌。我不知道如何做到这一点,我会很高兴收到一些关于如何编写我的 bash 脚本的建议以及一些提示。

【问题讨论】:

    标签: bash csv svg command-line inkscape


    【解决方案1】:

    试试 Inkscape + Generator 扩展:

    替换文本和数据以根据 SVG 模板和 CSV 文件自动生成文件(如 PDF、PNG、EPS、PS、JPG、SVG)。也可以通过 Debian 和 Ubuntu 存储库中的 apt 获得。

    http://wiki.colivre.net/Aurium/InkscapeGenerator

    用于测试和学习如何使用的示例文件集:

    https://inkscape.org/en/~Moini/%E2%98%85example-files-for-generator-extension

    【讨论】:

    【解决方案2】:

    使用 awk 的解决方案:

    # begin by reading the whole first template file at once
    BEGIN { RS = "^$" }
    FILENAME==ARGV[1] { # NR=1 is the entire template file
      template = $0
      # very conveniently we can now setup RS FPAT etc to apply to the next line for the next file.
      RS = "\r\n"
      # quoted CSV fields pattern
      FPAT = "([^,]*)|(\"[^\"]+\")"
    }
    
    FILENAME==ARGV[2] && NR==2 { # NR=2 is the first CSV line: column headers
      for (i = 1; i <= NF; i++) {
        headers[i] = $i
      }
    }
    
    FILENAME==ARGV[2] && NR>2 { # NR>2 is the rest of the CSV file records.
      # Strip quotes from quoted CSV values.
      for (i = 1; i <= NF; i++) {
        if (substr($i, 1, 1) == "\"") {
          $i = substr($i, 2, length($i) - 2)
        }
      }
    
      # Template Replacements
      result = template
      for (i = 1; i <= length(headers); i++) {
        gsub(headers[i], $i, result);
      }
    
      print result > "svg/" $1 ".svg"
    }
    

    这样使用: awk -f build.awk template.svg employees.csv

    其中 template.svg 是一个模板 SVG 文件,其中包含您要替换的字段名称。例如:

    <svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
     <g>
      <title>Layer 1</title>
      <rect width="100%" height="100%" fill="blue"/>
      <text id="svg_1" y="100" x="76" fill="#FFF">NAME</text>
     </g>
    </svg>
    

    此模板需要一个具有列标题“NAME”的 CSV 文件,但您可以在模板中添加与 CSV 中一样多的字段。这些文件将在“svg/”目录中输出,每行一个文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 2012-06-18
      • 2021-08-29
      • 2017-01-09
      相关资源
      最近更新 更多