Executing Python script for vs2017 C + + Project

CurrySerena 2022-05-14 16:19:47 阅读数:63


I solved even a small problem , I also want to write it down to witness my experience .

background :

One 、 Use libhuru Library generation pdf The report

Recently participated in some testing work , It is hoped that after the test, the test report can be automatically generated according to the data and images obtained from the test , At the beginning of the research, we found that the database for generating reports is libharu and libpdf,libharu It's an open source library, but it hasn't been maintained for a long time ,libpdf Commercial use requires authorization , Last choice use libharu, About use libharu library , The first is the corresponding libhpdf.lib Library compilation , Need to rely on zlib and libpng Two libraries , stay windows Next use cmake For compiling steps, please refer to this blog ,libharu The build process , I compiled according to this article 64 Bit libhpdfs.lib, On the based on libharu library , Routines that can solve the problem of loading tables and images in reports , You can refer to here be based on libharu Library generation pdf Reporting routines . I use the just compiled 64 Bit libhpdf.lib To compile this routine , In addition to loading png Image failure , The other functions I use are normal . I can only load jpg Images , But the image compression is serious , Not clear enough . Can't load png The image problem , I consulted the author of this routine , The author also tested my demo, Did not reproduce the problem I described , Later, I used what he provided libhpdf.lib, Successfully loaded png Images , Of course I have to be x86 In the mode of , And my big project is x64 Of , So this problem is very difficult to solve , On the Internet, I also saw many small partners react and use libhpdf The library could not be loaded successfully png The problem with images , The main problem lies in the following function , This function returns img It's empty , So it can't be in pdf Load image on file . Some netizens said that the allocation of memory failed , But I haven't found an effective way to solve this problem , I have to put it first .

img = HPDF_LoadPngImageFromFile(pdf, t.path.c_str());

At the end of last month , Follow line manager Reporting work , He was very dissatisfied with the image on the report I generated , In fact, poor people are not satisfied with themselves , It's really not clear , But this problem has not been solved , I am also very distressed , He once again suggested to me that I try to use python See if there can be a good solution , At the thought that I need to use python Do it again , I'm a little upset , I'm worried that the workload will be heavy , It's the end of the month , If it doesn't work , It will affect my performance this month . But there's no way , Who made me come back from the May Day holiday , Some don't want to make progress , Play with things and lose heart , I'm still Brag , Continue to search python Method . Just filled the membership a few days ago , Now a search , I found the following article , Use python Generate pictures and texts pdf The report , It makes me refreshing , A sigh , The money spent is really different , Much more efficient .

Two 、 Use python and reportlab Library generation pdf The report

1. windows Lower installation reportlab

First, make sure you have installed python Ha ha , My computer is installed python Version is 3.8.7

pip Version is 20.3.3


1. win+R
2. Input cmd # Open the command line window
3. Enter at the command line :pip install reportlab

If the installation fails, please check this article , Then you quit the software ( ha-ha , Do you know ), Repeat the installation .

3、 ... and 、 Refer to existing routines

My first contact with reportlab This library , Want to see satisfactory results in a very short time , I have to rely on the template of a county , So I found this article python And reportlab Generate PDF file . This example is of great reference value for beginners , But the only drawback is , The input when calling the function is not given , Because I? python Unfamiliar use , But after several tests , And research can also find out , The input of the function directory and List type . Because I have saved the test data to the local csv In file , So I read csv Write the data in the file into the table .

Four 、 stay C++ To execute in the works python Script

My working environment is win10,64 position VS2017, My test project uses C++ Written , What I want is after the test program runs , But before the program exits python Scripts are generated automatically pdf Test report , Then one of the problems I face is how to C++ To execute in the works python Script , Then you can directly use a simple, direct, rough and effective method . These lines of code are to execute what you specify python Script , If you need to C++ Some parameters in are passed to python Script , So directly command Just add the required parameters later .

std::string filename = "./test_report.py";
std::string command = "python ";
command += filename;

5、 ... and 、C++ Invocation in Engineering python Function or data exchange

1. First, you can refer to this article to configure the corresponding python Environmental Science Windows10 C++(VS2017) call python(python3.8)python To configure ,




2. Add environment variables


After the above environment is configured, you can test the example given by the blogger in the above link . One problem with my test results is , When the execution is ordinary python Script , That is the python When a third-party library is not called in the script , Can successfully execute , But when python The script has a library that calls a third party , Such as through import numpy as np call numpy library ,demo Medium pModule = PyImport_ImportModule("helloWorld"); Will go wrong , Back to pModule It's empty . And if you delete import numpy as np, Normal call again , It took me a whole day to solve this problem , Later, I thought I was just executing a separate script , No data exchange is required , It doesn't need to be in C++ Invocation in Engineering python The function in , Then solve the problem in the simplest and direct way , That's what I said above .

std::string filename = "./test_report.py";
std::string command = "python ";
command += filename;

If effective partners also encounter the above problems , And finally solved , Please advise , ha-ha .



版权声明:本文为[CurrySerena]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/134/202205140601480278.html