Coder Social home page Coder Social logo

Comments (5)

yangzhipeng1108 avatar yangzhipeng1108 commented on July 28, 2024 2

class Xls(BaseStrategy):
"""
通过将 Grid 另存为 xls 文件再读取的方式获取 grid 内容
"""

def __init__(self, tmp_folder: Optional[str] = None):
    """
    :param tmp_folder: 用于保持临时文件的文件夹
    """
    super().__init__()
    self.tmp_folder = tmp_folder

def get(self, control_id: int) -> List[Dict]:
    grid = self._get_grid(control_id)

    # ctrl+s 保存 grid 内容为 xls 文件
    self._set_foreground(grid)  # setFocus buggy, instead of SetForegroundWindow
    grid.type_keys("^s", set_foreground=False)
    # count = 10
    # while count > 0:
    #     if self._trader.is_exist_pop_dialog():
    #         break
    #     self._trader.wait(0.2)
    #     count -= 1

    if  (self._trader.app.top_window().window(class_name="Static", title_re="验证码").exists(timeout=1)): #检查是否有验证码输入Window
        file_path = "tmp.png"
        count = 5
        found = False
        while count > 0:
            self._trader.app.top_window().window(
                control_id=0x965, class_name="Static"
            ).capture_as_image().save(
                file_path
            )  # 保存验证码

            captcha_num = captcha_recognize(file_path).strip()  # 识别验证码
            captcha_num = "".join(captcha_num.split())
            logger.info("captcha result-->" + captcha_num)
            if len(captcha_num) == 4:

                editor = self._trader.app.top_window().window(
                    control_id=0x964, class_name="Edit")
                editor.select()
                editor.type_keys(captcha_num)

                self._trader.app.top_window().set_focus()
                pywinauto.keyboard.SendKeys("{ENTER}")  # 模拟发送enter,点击确定
                if self._trader.app.window(title='另存为').exists(timeout=1):
                    found = True
                    break
                logger.info(f"captcha result:{captcha_num} error")
            count -= 1
            self._trader.wait(0.1)
            self._trader.app.top_window().window(
                control_id=0x965, class_name="Static"
            ).click()
        if not found:
            self._trader.app.top_window().Button2.click()  # 点击取消

    temp_path = tempfile.mktemp(suffix=".xls", dir=self.tmp_folder)
    self._set_foreground(self._trader.app.top_window())

    # alt+s保存,alt+y替换已存在的文件
    self._trader.app.top_window().Edit1.set_edit_text(temp_path)
    self._trader.wait(0.1)
    self._trader.app.top_window().type_keys("%{s}%{y}", set_foreground=False)
    # Wait until file save complete otherwise pandas can not find file
    self._trader.wait(0.2)
    if self._trader.is_exist_pop_dialog():
        self._trader.app.top_window().Button2.click()
        self._trader.wait(0.2)

    return self._format_grid_data(temp_path)

def _format_grid_data(self, data: str) -> List[Dict]:
    with open(data, encoding="gbk", errors="replace") as f:
        content = f.read()

    df = pd.read_csv(
        StringIO(content),
        delimiter="\t",
        dtype=self._trader.config.GRID_DTYPE,
        na_filter=False,
    )
    return df.to_dict("records")

from easytrader.

huaixu1358 avatar huaixu1358 commented on July 28, 2024

方便给一份完整代码我吗

from easytrader.

Ziven-L avatar Ziven-L commented on July 28, 2024

您好,可以给我一份完整代码吗?

from easytrader.

skyformat99 avatar skyformat99 commented on July 28, 2024

推送pr啊

from easytrader.

yangzhipeng1108 avatar yangzhipeng1108 commented on July 28, 2024

Tesseract 使用参考这两文章https://blog.csdn.net/u010454030/article/details/80515501https://blog.csdn.net/wang_hugh/article/details/80760940

from easytrader.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.