自动化在Excel加载.csv文件的步骤与Python

问题描述 投票:-1回答:1

我要加载很多.csv文件到Excel定期,像以图示出here (till step 9) -

Excel中 - >数据 - >从文本 - “导航到它的位置,然后单击导入选择文件”> - >的3文本导入向导步骤1。

这些都我已经写一个小python脚本,自动化的步骤,我使Python按快捷键(想象一个没有鼠标操作,按下快捷键在Excel中进行导航)。

import pyautogui
from pywinauto.application import Application
app = Application().start('C:\Program Files (x86)\Microsoft Office\Office12\EXCEL.exe')
pyautogui.press('alt')
pyautogui.press('a')
pyautogui.press('f')
pyautogui.press('t')
pyautogui.typewrite(r'C:\Users\Jo\Revenue.txt')
pyautogui.press('enter')

运行上面的Python脚本后,我提出这个导入向导窗口 -

Import Wizard

现在,我怎样才能使蟒蛇从下拉菜单中选择65001 : Unicode (UTF-8)?我怎么能在我的Python代码指定选择这个编码?任何输入将高度赞赏。

python excel automation
1个回答
0
投票

这是我怎么会去了解它。添加UTF-8 with BOM到文件开头会自动指示精益求精,file origin下拉列表选择UTF:

import codecs
import pyautogui

from pywinauto.application import Application
from pywinauto.keyboard import SendKeys

from time import sleep

filename = r'C:\path\excel_automation\data1.txt'
global content

def add_utf8_bom(filename):
    with codecs.open(filename, 'r', 'utf-8') as f:
        content = f.read()
    with codecs.open(filename, 'w', 'utf-8') as f2:
        f2.write('\ufeff')
        f2.write(content)

add_utf8_bom(filename)
app = Application().start(r'C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE')
sleep(1)
#SendKeys('^n') #uncomment for excel 2016 or higher
sleep(1)
pyautogui.press('alt')
pyautogui.press('a')
pyautogui.press('f')
pyautogui.press('t')
pyautogui.typewrite(filename)
pyautogui.press('enter')

信用:https://stackoverflow.com/a/29900075/4551984

© www.soinside.com 2019 - 2024. All rights reserved.