因为业务的需要,要通过java代码去调用python脚本,由于python脚本里面引入了很多第三方库,所以java代码去调用的时候返回都是1、9009,后来百度了一下得把python的sdk写入到代码里,然后我对此改动了一下。
1.python脚本
import docx from docx.document import Document from docx.oxml.table import CT_Tbl from docx.oxml.text.paragraph import CT_P from docx.table import _Cell, Table from docx.text.paragraph import Paragraph import re def iter_block_items(parent): """ Yield each paragraph and table child within *parent*, in document order. Each returned value is an instance of either Table or Paragraph. """ if isinstance(parent, Document): parent_elm = parent.element.body elif isinstance(parent, _Cell): parent_elm = parent._tc else: raise ValueError("something's not right") current_paragraph = "" for child in parent_elm.iterchildren(): if isinstance(child, CT_P): # yield Paragraph(child) p = Paragraph(child, parent) # regx if re.search(r'\d*\|\d{1,9}', p.text): chapters = p.text.split("|") current_paragraph = chapters[1].strip() # print(current_paragraph) # print(p.text) elif isinstance(child, CT_Tbl): print("当前章节:"+current_paragraph) table = Table(child, parent) for row in table.rows: for cell in row.cells: if cell.text and re.search(r'=(ds|ds1|ds2)',cell.text): print(cell.text) if __name__ == '__main__': # 仅仅用来测试python对word文件的解析 file_path = "C:\\luanht\\develop\\IdeaProject\\pythonTest\\test\\test.docx" # doc = Document(file_path) doc = docx.Document(file_path) iter_block_items(doc)
2.先确认python的sdk位置
3.java代码
import java.io.*; public class JavaToPython { public static void main(String[] args) throws IOException ,InterruptedException { //入参 String arg1 = ""; String arg2 = ""; //当前python的SDK位置,和脚本的绝对路径 String[] arguments = new String[] {"C:\\Users\\lht\\AppData\\Local\\Programs\\Python\\Python310\\python.exe", "C:\\luanht\\develop\\IdeaProject\\pythonTest\\test\\doc_structure.py" ,arg1,arg2}; try { Process process = Runtime.getRuntime().exec(arguments); BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK")); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); //java代码中的process.waitFor()返回值为0表示我们调用python脚本成功, //返回值为1表示调用python脚本失败,这和我们通常意义上见到的0与1定义正好相反 int re = process.waitFor(); System.out.println(re); } catch (Exception e) { e.printStackTrace(); } } }