Browse Source

Merge branch 'master' of http://8.136.199.33:3000/eta_server/eta_python

xyxie 1 year ago
parent
commit
eb331e4160
5 changed files with 102 additions and 42 deletions
  1. 79 8
      eta_report2img/html2img_ping.py
  2. 2 5
      eta_report2img/img_pdf_png.py
  3. 2 1
      html2img/main.py
  4. 10 1
      rpa_mysteel_refresh.py
  5. 9 27
      wind_api.py

+ 79 - 8
eta_report2img/html2img_ping.py

@@ -3,6 +3,8 @@ from time import sleep
 from selenium import webdriver
 from selenium.webdriver.chrome.service import Service
 import img_pdf_png
+import os
+from PIL import Image
 
 
 def scroll_page(driver):
@@ -19,7 +21,7 @@ def scroll_page(driver):
         for j in range(n1):
             # print(r"j:{}".format(j))
             if j == n1 - 1 and last_height > 0:  # 截取尾部
-                driver.execute_script("window.scrollBy(0,{})".format(page_height))  # 滚动条滚到底
+                driver.execute_script("window.scrollBy(0,{})".format(last_height))  # 滚动条滚到底
                 print("滚到底了")
             else:
                 driver.execute_script("window.scrollBy(0,{})".format(web_height))
@@ -45,26 +47,95 @@ def html2img(driver, image_folder):
     print("当前窗口网页高度{}".format(web_height))
     last_height = page_height % web_height
     print("底部多出来的部分{}".format(last_height))
-    margin_flag = False
+
+    is_end = 0
     if n1 == 0 or (n1 == 1 and last_height == 0):  # 判断是否需要滚动
-        driver.get_screenshot_as_file(r'{}/{}.png'.format(image_folder, '0'))  # 指定截图保存位置
+        # 去除留白部分, 一般情况下也不会只写一页报告且留那么多空白
+        crop_height = web_height - page_height
+        if crop_height > 0:
+            origin_last_img = r'{}/{}.png'.format(image_folder, 1000)
+            new_last_img = r'{}/{}.png'.format(image_folder, 0)
+            driver.save_screenshot(origin_last_img)
+            crop_img_botton(origin_last_img, new_last_img, page_height)
+        else:
+            driver.get_screenshot_as_file(r'{}/{}.png'.format(image_folder, '0'))
     else:
         j = 0
         while j <= n1:
             if j == 0:
                 driver.execute_script("window.scrollBy(0,0)")
                 # print(0)
-            elif j == n1 and last_height > 0:  # 截取尾部
-                driver.execute_script("$('#app #resetcss').css('margin-bottom', '{}px')".format(web_height))
-                driver.execute_script("window.scrollBy(0,{})".format(web_height))  # 滚动条滚到底
+            elif j == n1:
+                # print(f'n1 is {n1}')
+                if last_height > 0:
+                    # driver.execute_script("$('#app #resetcss').css('margin-bottom', '{}px')".format(web_height))
+                    driver.execute_script("window.scrollBy(0,{})".format(last_height))  # 滚动条滚到底
+
+                    # 最后一屏先进行一次保存
+                    sleep(1)
+                    origin_last_img = r'{}/{}.png'.format(image_folder, j + 1000)
+                    driver.save_screenshot(origin_last_img)
+
+                    # 截取上面重叠的一部分并保存截取后的图片
+                    new_last_img = r'{}/{}.png'.format(image_folder, j)
+                    crop_height = web_height - last_height
+                    crop_img_top(origin_last_img, new_last_img, crop_height)
+
+                is_end = 1
                 print("拉到底拉")
             else:
                 driver.execute_script("window.scrollBy(0,{})".format(web_height))
-            sleep(1)
-            driver.save_screenshot(r'{}/{}.png'.format(image_folder, j))  # 截屏
+
+            if is_end == 0:
+                sleep(1)
+                driver.save_screenshot(r'{}/{}.png'.format(image_folder, j))  # 截屏
             j = j + 1
 
 
+# crop_img_top 裁掉图片上面的部分保留剩下的部分
+def crop_img_top(image_path, output_path, crop_height):
+    # 打开图像文件
+    img = Image.open(image_path)
+
+    # 获取图像的宽度和高度
+    width, height = img.size
+
+    # 计算裁剪的起始坐标
+    start_x = 0
+    start_y = min(height, crop_height)  # 保证不超出图像下边界
+
+    # 裁剪图像,仅保留上半部分
+    cropped_img = img.crop((start_x, start_y, start_x + width, start_y + (height - crop_height)))
+
+    # 保存裁剪后的图像
+    cropped_img.save(output_path)
+
+    # 删除原始图像
+    os.remove(image_path)
+
+
+# crop_img_botton 裁掉图片下面的部分保留上面的部分
+def crop_img_botton(image_path, output_path, keep_height):
+    # 打开图像文件
+    img = Image.open(image_path)
+
+    # 获取图像的宽度和高度
+    width, height = img.size
+
+    # 计算保留部分的起始坐标
+    start_x = 0
+    start_y = 0
+
+    # 裁剪图像,保留从左上角开始指定高度的部分
+    cropped_img = img.crop((start_x, start_y, start_x + width, start_y + keep_height))
+
+    # 保存裁剪后的图像
+    cropped_img.save(output_path)
+
+    # 删除原始图像
+    os.remove(image_path)
+
+
 # 调用截图函数
 if __name__ == "__main__":
     # 创建一个 Chrome WebDriver 实例

+ 2 - 5
eta_report2img/img_pdf_png.py

@@ -35,13 +35,10 @@ def merge_images(image_folder, output_file, file_name, output_type, n=1):
     now_height = 0
     for i in range(image_count):
         img = Image.open(os.path.join(image_folder, target_img[i]))
-        img_size0 = int(img.size[0] / n)
-        img_size1 = int(img.size[1] / n)
-        img = img.resize((img_size0, img_size1))
-        if i != 0:
-            now_height += img.size[1]
         # 纵向拼接图片
         new_img.paste(img, (0, now_height))
+        # 当前粘贴高度自增已拼接的图片高度
+        now_height += img.size[1]
 
     # 默认两种类型都生成
     if output_type == "":

+ 2 - 1
html2img/main.py

@@ -16,7 +16,8 @@ host = '127.0.0.1'
 debug = True
 
 # 工具配置
-tool_path = r'E:\wkhtmltopdf\bin\wkhtmltoimage.exe'
+tool_path = r'/usr/local/bin/wkhtmltoimage'
+# tool_path = r'E:\wkhtmltopdf\bin\wkhtmltoimage.exe'
 
 # OSS配置
 oss_end_point = 'oss-cn-shanghai.aliyuncs.com'

+ 10 - 1
rpa_mysteel_refresh.py

@@ -13,6 +13,9 @@ import datetime
 hug.API(__name__).http.output_format = hug.output_format.json
 
 exePath = r"C:\Program Files (x86)\Kingsoft\WPS Office\11.8.2.12085\office6\et"
+# exePath = exePath = r"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE"
+# exePath = exePath = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
+
 
 # 顶部钢联按钮的x,y位置
 gl_button_x = 645
@@ -44,7 +47,7 @@ def mysteel_chemical_refresh(FilePath):
     # FilePath = urllib.parse.unquote(FilePath)
     FilePath = FilePath.replace('"', '')
 
-    # pyautogui.hotkey('win', 'd')
+    pyautogui.hotkey('win', 'd')
     # time.sleep(2)
     #
     # moveX = 1100
@@ -107,6 +110,12 @@ def mysteel_chemical_refresh(FilePath):
 
             except Exception as e:
                 print("打开", fileFullPath, "失败:", e)
+
+                ## 失败的话,截图记录下原因
+                screenshot = pyautogui.screenshot()
+                imgPath = datetime.datetime.now().strftime("%Y%m%d%H%M%S")+"_screenshot_img.png"
+                screenshot.save(imgPath)
+
                 hwnd = win32gui.GetForegroundWindow()
                 found_title = win32gui.GetWindowText(hwnd)
                 print("窗口名称:", found_title)

+ 9 - 27
wind_api.py

@@ -49,9 +49,9 @@ def GetEdbDataByWind(EdbCode, StartDate, EndDate):
     return result
 
 
-@hug.get('/edbInfo/wind/future_good')
-def GetFutureGoodEdbDataByWind(FutureGoodEdbCode, StartDate, EndDate):
-    print("GetFutureGoodEdbDataByWind:", FutureGoodEdbCode)
+@hug.get('/edbInfo/wind/wsd')
+def GetEdbDataWindWsd(StockCode,EdbCode, StartDate, EndDate):
+    print("GetEdbDataByWind:", EdbCode)
     isConnected = w.isconnected()
     print("isconnected")
     print(isConnected)
@@ -62,38 +62,20 @@ def GetFutureGoodEdbDataByWind(FutureGoodEdbCode, StartDate, EndDate):
         if isConnected == False:
             return "{'ErrMsg':'启动Wind接口失败'}"
 
-    print("getdata")
     option = "Fill=Previous"
-    data = w.edb(FutureGoodEdbCode, StartDate, EndDate, option)
-    data = w.wsd(FutureGoodEdbCode, "trade_code,open,high,low,close,volume,amt,oi,settle", StartDate, EndDate, option)
-    print("wind data")
-    print(data)
-    df = pd.DataFrame()
+    wsd_data = w.wsd(StockCode,EdbCode, StartDate, EndDate, option)
 
-    if data.ErrorCode == -40521010: # Internet Timeout 超时退出
+    if wsd_data.ErrorCode == -40521010: # Internet Timeout 超时退出
         os._exit(0)
         return "a"
-
-    df['DT'] = data.Times
-    df['TRADE_CODE'] = data.Data[0]
-    df['OPEN'] = data.Data[1]
-    df['HIGH'] = data.Data[2]
-    df['LOW'] = data.Data[3]
-    df['CLOSE'] = data.Data[4]
-    df['VOLUME'] = data.Data[5]
-    df['AMT'] = data.Data[6]
-    df['OI'] = data.Data[7]
-    df['SETTLE'] = data.Data[8]
-    df['ErrorCode'] = data.ErrorCode
-    df = df[['DT', 'TRADE_CODE', 'OPEN', 'HIGH', 'LOW', 'CLOSE', 'VOLUME', 'AMT', 'OI', 'SETTLE', 'ErrorCode']]
-    df = df.dropna()
-    json_data = df.to_json()
-    # w.stop()
-    print("wind data end")
+   
+    fm=pd.DataFrame(wsd_data.Data,index=wsd_data.Fields,columns=wsd_data.Times)
+    json_data=fm.to_json()
     result = json.loads(json_data)
     return result
 
 
+
 if __name__ == "__main__":
     # wind 登录
     wStart = w.start()