利用Jacob将 lrc 文件转换为 docx 文件并整理格式

  • 问题描述
    • 最近听SVOA,很多单词不认识,想拿对应的 lrc 文件打印下来,无奈 lrc 文件格式太乱,直接打印耗费纸张不说,看着实在不爽;手动整理吧,量太大,400多个文件
    • 于是想:搞IT的,能不能写段程序让电脑把这个事情做了
    • 于是又了下面的代码,希望可以帮到一些有同样需求的童鞋
  • 要求:


    • jacob-1.17-x64.dll 是给 64 位机器用的
    • jacog-1.17-x86.dll就是给 32 机器用的了
    • 选择对应你机器的文件放在 C:\Windows\System32 下
    • 将 jacog.jar 引入项目
  • 直接贴代码:
    • package lyric2doc;
      
      import java.util.List;
      
      import doc.Doc;
      
      import lyric.Lyric;
      
      public class Lyric2Doc {
          
          public static String nullityTag = "00:00.00";
          
          public static String titleFontSize = "12";
          public static String bodyFontSize = "11";
          
          public static int alignLeft = 0;
          public static int alignCenter = 1;
          public static int alignRight= 2;
          public static int alignJustify = 3;
          public static int alignDistributed = 4;
          
          //default 1.0
          public static int lineSpace0 = 0;
          //1.5
          public static int lineSpace1 = 1;
          //2.0
          public static int lineSpace2 = 2;
          //minimum
          public static int lineSpace3 = 3;
          //fixed value
          public static int lineSpace4 = 4;
          
          public static void main(String[] args) {
              for (String fileAbsoluteName : Lyric.fileAbsoluteNameList) {
                  String fileName = fileAbsoluteName
                          .substring(fileAbsoluteName.lastIndexOf("\\") + 1, fileAbsoluteName.length() - 4);
                  
                  Doc.createNewDocument();
                  
                  Doc.writeIntoAString("Special VOA 2013", "Arial", titleFontSize, "0", true, false, false, 0, 1);
                  Doc.writeIntoAString("\n", "Arial", titleFontSize, "0", true, false, false, 0, 1);
                  Doc.writeIntoAString(fileName, "Arial", titleFontSize, "0", true, false, false, 0, 1);
                  Doc.writeIntoAString("\n", "Arial", titleFontSize, "0", true, false, false, 0, 1);
                  
                  List<String> lineList = Lyric.readLines(fileAbsoluteName);
                  for (String string : lineList) {
                      String prefixTwo2Nine = string.substring(1, 9);
                      if (String.valueOf(prefixTwo2Nine.charAt(0)).matches("^[0-9]")) {
                          if (!prefixTwo2Nine.equals(nullityTag)) {
                              Doc.writeIntoAString(string.substring(10, string.length()), "Arial", bodyFontSize, "0", false, false, false, 0, 1);
                              String lastChar = String.valueOf(string.charAt(string.length() - 1));
                              if (lastChar.matches("[a-zA-Z]")
                                      || lastChar.equals(",")
                                      || lastChar.equals("\"")) {
                                  Doc.writeIntoAString(" ", "Arial", bodyFontSize, "0", false, false, false, 0, 1);
                              } else if (lastChar.equals(".")
                                      || lastChar.equals("!")) {
                                  Doc.writeIntoAString("\n", "Arial", bodyFontSize, "0", false, false, false, 0, 1);
                              }
                          }
                      }
                  }
                  
                  Doc.saveDocumentAs(fileName);
                  Doc.closeDocument();
              }
              
              Doc.closeMsWordApp();
          }
      
      }
    • package lyric;
      
      import java.io.BufferedReader;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.util.ArrayList;
      import java.util.List;
      
      public class Lyric {
          
          public static List<String> fileAbsoluteNameList = new ArrayList<String>();
          
          private static String path = "F:\\Music\\Lyrics";
          
          static {
              getAllEnglishAbsoluteFileName();
          }
          
          private static void getAllEnglishAbsoluteFileName() {
              
              File file = new File(path);
              
              File[] fileArray = file.listFiles();
              
              for (int i = 0; i < fileArray.length; i ++) {
                  if (fileArray[i].isFile()) {
                      if (fileArray[i].getName().substring(0, 1).matches("[a-zA-Z]")) {
                          fileAbsoluteNameList.add(fileArray[i].getAbsolutePath());
                      }
                  }
              }
          }
          
          public static List<String> readLines(String fileName) {
              List<String> lineList = new ArrayList<String>();
              
              File file = new File(fileName);
              
              InputStreamReader isr = null;
              BufferedReader br = null;
              try {
                  isr = new InputStreamReader(new FileInputStream(file), "GB2312");
                  br = new BufferedReader(isr);
                  
                  String tempString = null;
                  
                  while ((tempString = br.readLine()) != null) {
                      if (tempString.trim().length() > 10) {
                          String newString = tempString
                                  .replace("。", ".")
                                  .replace("“", "\"")
                                  .replace("”", "\"")
                                  .trim();
                          lineList.add(newString);
                      }
                  }
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              } finally {
                  if (br != null) {
                      try {
                          br.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
              
              return lineList;
          }
      
      }
    • package doc;
      
      import com.jacob.activeX.ActiveXComponent;
      import com.jacob.com.Dispatch;
      import com.jacob.com.Variant;
      
      public class Doc {
          
          //Decide if the process is visible
          private static boolean ifWordVisible = true;
          
          private static String savePathPrefix = "F:\\Music\\Special_VOA\\LyricPrint\\";
          
          private static ActiveXComponent MsWordApp = null;
          
          private static Dispatch documents = null;
          
          //Maintain the current global document object
          private static Dispatch document = null;
          
          static {
              if (MsWordApp == null) {
                  MsWordApp = new ActiveXComponent("Word.Application");
              }
              
              Dispatch.put(MsWordApp, "Visible", ifWordVisible);
              
              documents = Dispatch.get(MsWordApp, "Documents").toDispatch();
          }
          
          public static void createNewDocument() {
              document = Dispatch.call(documents, "Add").toDispatch();
          }
          
          public static void saveDocumentAs(String fileName) {
              Dispatch.call(document, "SaveAs", savePathPrefix + fileName + ".docx");
          }
          
          public static void closeDocument() {
              Dispatch.call(document, "Close", new Variant(0));
              document = null;
          }
          
          public static void closeMsWordApp() {
              Dispatch.call(MsWordApp, "Quit");
              documents = null;
              MsWordApp = null;
          }
          
          public static void writeIntoAString(String string,
                  String fontName,
                  String fontSize,
                  String color,
                  boolean bold,
                  boolean italic,
                  boolean underline,
                  int align,
                  int lineSpace) {
              Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
              Dispatch.call(selection, "MoveRight", new Variant(1), new Variant(1));
              Dispatch.put(selection, "Text", string);
              
              setFont(selection, fontName, fontSize, color, bold, italic, underline);
              
              Dispatch alignment = Dispatch.get(selection, "ParagraphFormat").toDispatch();
              Dispatch.put(alignment, "Alignment" , align);
              Dispatch.put(alignment, "LineSpacingRule" ,  new  Variant(lineSpace));
              
              Dispatch.call(selection, "MoveRight", new Variant(1), new Variant(1));
          }
          
          private static void setFont(Dispatch selection,
                  String fontName,
                  String fontSize,
                  String color,
                  boolean bold,
                  boolean italic,
                  boolean underLine) {
              Dispatch font = Dispatch.get(selection, "Font").toDispatch();
              Dispatch.put(font, "Name", new Variant(fontName));
              Dispatch.put(font, "Size", fontSize);
              Dispatch.put(font, "Color", color);
              Dispatch.put(font, "Bold", new Variant(bold));
              Dispatch.put(font, "Italic", new Variant(italic));
              Dispatch.put(font, "Underline", new Variant(underLine));
          }
          
      }
  • 欢迎讨论交流
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值