用 aspose words 把 Docx/Doc 转换为 TXT, 并保留表格格式
Java
import com.aspose.words.Document;
import com.aspose.words.TxtSaveOptions;
import java.io.File;
import java.nio.file.Paths;
//import java.nio.file.Path;
import java.io.FileOutputStream;
public class Pdf2Txt {
public static void main(String[] args)throws Exception {
String input_path = args[0];
String output_dir = args[1];
txt(input_path, output_dir);
private static void txt(String input_path, String output_dir)throws Exception {
TxtSaveOptions txtSaveOptions = new TxtSaveOptions();
String input_name = Paths.get(input_path).getFileName().toString();
// System.out.println("inputfilename: " + input_name);
String output_path = Paths.get(output_dir).resolve(input_name).toString() + ".txt";
System.out.println("output: " + output_path);
txtSaveOptions.setParagraphBreak("\n"); // 设置换行符
//txtSaveOptions.setExportGeneratorName(false);
txtSaveOptions.setPreserveTableLayout(true); //保留表格风格
txtSaveOptions.setPrettyFormat(true); // 格式化
txtSaveOptions.setUseHighQualityRendering(true); //高质量渲染
Document doc = new Document(input_path);
doc.save(output_path, txtSaveOptions);
}
}
Python
- 安装
pip install aspose-words
- 代码
import sys
import os.path
import aspose.words as aw
op = aw.saving.TxtSaveOptions()
op.export_generator_name = False
op.paragraph_break = '\n'
op.preserve_table_layout = True
op.pretty_format = True
op.use_high_quality_rendering = True
doc = aw.Document(sys.argv[0])
filename = os.path.basename(sys.argv[0])
doc.save(os.path.join(sys.argv[1], filename) + '.txt', op)