java调用python脚本(含第三方库)

因为业务的需要,要通过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();
        }
    }
}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值