" name="sm-site-verification"/>
侧边栏壁纸
博主头像
PySuper 博主等级

千里之行,始于足下

  • 累计撰写 206 篇文章
  • 累计创建 14 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

Python 调用C、.dll

PySuper
2019-12-23 / 0 评论 / 0 点赞 / 17 阅读 / 0 字
温馨提示:
本文最后更新于2024-05-28,若内容或图片失效,请留言反馈。 所有牛逼的人都有一段苦逼的岁月。 但是你只要像SB一样去坚持,终将牛逼!!! ✊✊✊

配置环境

  • 其实不同的.dl适配的情况是不一样的,这里就举一个例子:
    • 使用的.net framework必须要4.8的
    • 这种情况,比较好解决,我们直接从官网下载安装相应的版本就好了
  • import clr
    • 在Pythonnet中的,不是我们pip install clr安装的包
    • 我们直接安装pythonnet, 然后在py文件中直接导入当前这个clr就可以了
    • Pycharm中会显示红色,当时不影响正常使用的

实例

import sys
import clr
import h5py
import numpy as np

sys.path.append("C:\\Program Files (x86)\\HEAD ArtemiS SUITE 11.0")
clr.FindAssembly('HEADacoustics.API.Hdf.dll')
from HEADacoustics.API.Hdf import *


class ParseHDFFILE():
    def __init__(self, file_path):
        self.file_path = file_path
        self.license = License.Create()
        self.chs = StreamReader.Create(file_path).GetChannels()
        self.chs = StreamReader.Create(file_path).ChannelInfo.name  # 获取通道名

    # 获取每个channel的数据
    def get_nth_channel(self):
        for cb in self.chs:
            n_frames = self.chs[cb].NumberOfSamples  # 获取样本数量
            data_single = self.chs[cb].GetData(0, n_frames)
            data_ = []

            # get data
            for i in range(0, n_frames, 1):
                data_.append(data_single[i])
            data_ = np.float64(data_)  # 把数据 经float转换类型后,存放到data_中

            # 横坐标--时间
            time_abscissa = self.chs[0].EquidistantAbscissa
            time_ = np.arange(
                time_abscissa.FirstValue,
                time_abscissa.LastValue + time_abscissa.DeltaValue,
                time_abscissa.DeltaValue
            )
            yield time_, data_

    # 把数据转存为可读HDF
    def save_hdf(self, time_, data_):
        save_file = h5py.File(self.file_path, 'w')

        for num in range(self.chs.Length):
            save_file['time' + str(num)] = time_
            save_file['data' + str(num)] = data_

    # 读取HDF文件
    def read_hdf(self):
        read_file = h5py.File(self.file_path, 'r')

        for key in read_file.keys():
            print(read_file[key].name)
            print(read_file[key].shape)
            print(read_file[key].value)


for time, data in ParseHDFFILE("filepath").get_nth_channel():
    ParseHDFFILE("filepath").save_hdf(time, data)
ParseHDFFILE("filepath").read_hdf()

处理多个文件

import os
import re
import sys
import clr
import h5py
import numpy as np
from multiprocessing import Pool, cpu_count

sys.path.append("C:\\Program Files (x86)\\Test")
clr.FindAssembly('Test.API.Hdf.dll')
from Test.API.Hdf import *

# save_path = "D_LL\\Test_HDF"
save_path = "Y:\\Info"


class ParseHDFFILE():
    def __init__(self, file_path, save_path):
        self.license = License.Create()
        self.chs = StreamReader.Create(file_path).GetChannels()

    # 获取每个channel的数据
    def get_nth_channel(self):
        for cb in range(self.chs.Length):
            channel_name = self.chs[cb].ChannelInfo.Name  # 数据名
            n_frames = self.chs[cb].NumberOfSamples  # 获取样本数量
            data_single = self.chs[cb].GetData(0, n_frames)
            data_ = []

            # get data
            for i in range(0, n_frames, 1):
                data_.append(data_single[i])
            data_ = np.float64(data_)  # 把数据 经float转换类型后,存放到data_中

            # 横坐标--时间
            time_abscissa = self.chs[0].EquidistantAbscissa
            time_ = np.arange(
                time_abscissa.FirstValue,
                time_abscissa.LastValue + time_abscissa.DeltaValue,
                time_abscissa.DeltaValue
            )
            yield channel_name, time_, data_

    # 把数据转存为可读HDF
    def save_hdf(self, i, filename, channel_name, time_, data_):
        try:
            save_file = h5py.File(save_path + filename, 'a')
            save_file['data' + "_" + channel_name] = data_
            if i < 2:
                save_file['time'] = time_
        except FileExistsError as e:
            print(e)

    # 读取HDF文件
    def read_hdf(self):
        read_file = h5py.File(save_path, 'r')
        for key in read_file.keys():
            print(read_file[key].name, read_file[key].shape, read_file[key].value)


def process_file(current_file_path):
    parse_file = ParseHDFFILE(current_file_path, save_path)
    filename = re.match(r'Y:\\Info\\(.*\.hdf)', current_file_path).group(1)

    print(filename)

    i = 1
    for channel_name, time, data in parse_file.get_nth_channel():
        # 存储数据
        parse_file.save_hdf(i, filename, channel_name, time, data)
        i += 1
        # break

    # 读取数据
    # parse_file.read_hdf()


def start_process(file_list):
    # print(os.getpid(), cpu_count())
    pool = Pool(cpu_count())
    for current_file_path in file_list:
        pool.apply_async(process_file, args=("Y:\\Info\\" + current_file_path,))
    pool.close()
    pool.join()


# 获取指定路径下的全部文件列表
if __name__ == '__main__':
    file_list = os.listdir('Y:\\Info\\')
    start_process(file_list)

视图函数

import six
import copy
from pptx import Presentation


def duplicate_slide(pres, index):
    """
    从前端获取当
    :param pres:
    :param index:
    :return:
    """
    template = pres.slides[3]  # 选用的模板(要复制的幻灯片)
    blank_slide_layout = pres.slide_layouts[index]
    copied_slide = pres.slides.add_slide(blank_slide_layout)

    # 修改文本框内容
    # copied_slide.shapes[0].text= "title"
    # copied_slide.shapes[1].text= "爱仕达所多"

    # 添加图片
    # left, top, width, height = Inches(0.25), Inches(1.8), Inches(7.7), Inches(4.8)
    # pic = prs.slides[3].shapes.add_picture('image/zxt_save_data.jpg', left, top, width, height)

    # 填充幻灯片文字信息
    for shp in template.shapes:
        el = shp.element
        newel = copy.deepcopy(el)
        copied_slide.shapes._spTree.insert_element_before(newel)  # , 'p:extLst'

    # 填充幻灯片中的图片
    for _, value in six.iteritems(template.part.rels):
        if "notesSlide" not in value.reltype:
            copied_slide.part.rels.add_relationship(value.reltype, value._target, value.rId)


data = [
    {
        "item": "base",
        "title": "title",
        "gear": "F2",
        "ac_de": "VZ"
    }, {
        "item": "base",
        "title": "title",
        "gear": "F2",
        "ac_de": "VS"
    }

]


class ParsePPT():
    def __init__(self, item):
        """
        处理前端的数据
        :param item:前端传来的图片的位置信息
        """
        pass

    def parse_ppt(self):
        """
        通过处理后的前端数据,合成规定的报告
        :return:
        """
        pass

    def save_ppt(self):
        """
        保存最终的PPT文件
        :return:
        """
        pass


if __name__ == '__main__':
    """
    前端只返回一张幻灯片,一次生成一张,依次添加
    {[1,2,3,4]}

    # prs = Presentation("PPT/A.pptx")
    # duplicate_slide(prs, 0)
    # prs.save('zzz.pptx')
    
    """

    """
    1. 接受前端数据 
    2. 图片放到PPT中
        2.1 获取该图片的位置信息
        2.2 把图片放置到指定的位置
            2.3.1 对于4~6张图片作出区分
            2.3.2 不同数量的图片放置逻辑
            2.3.4 多出来的图片放置到当前页的下一页(不是整个PPT的最后)
        2.3 接受的应该是前端很多个文件,一次性生成所有的幻灯片
    3. 一键下载PDF + PPT
        3.1 讲前端的PDF,后端的PPT,下载到用户指定的目录(Download)      
    4. 前后端调试接口
    """
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区