Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 出现异常(..)-“Microsoft Excel”、“另存为…”。。。类失败'_Python 3.x - Fatal编程技术网

Python 3.x 出现异常(..)-“Microsoft Excel”、“另存为…”。。。类失败'

Python 3.x 出现异常(..)-“Microsoft Excel”、“另存为…”。。。类失败',python-3.x,Python 3.x,这段代码基本上完成了它需要做的事情。但它有时确实会抛出错误,而我希望代码能够在所有必要数据都存在的情况下工作 因此,它所做的是:1读入用户2向Excel仪表板添加信息,例如标题信息、词云和配置文件图片 然后保存Excel文件。它有时几乎是随机地给出一个错误:pywintypes.com_error:-2147352567,“发生异常”,0,“Microsoft Excel”,“工作簿类的打开方法失败”,“xlmain11.chm”,0,-2146827284,无。代码的哪一部分会导致这种情况 i

这段代码基本上完成了它需要做的事情。但它有时确实会抛出错误,而我希望代码能够在所有必要数据都存在的情况下工作

因此,它所做的是:1读入用户2向Excel仪表板添加信息,例如标题信息、词云和配置文件图片

然后保存Excel文件。它有时几乎是随机地给出一个错误:pywintypes.com_error:-2147352567,“发生异常”,0,“Microsoft Excel”,“工作簿类的打开方法失败”,“xlmain11.chm”,0,-2146827284,无。代码的哪一部分会导致这种情况

import os
import xlwings as xw
import pandas as pd
import openpyxl


def get_users(file_name):
    """Read all the users from the csv file."""
    users = []
    f = open(file_name, 'r')
    for line  in f:
        user = line.strip().split(',')
        screen_name = user[0]
        users.append(screen_name)
    f.close()
    return users


def read_csv_file(file_name):
    """Return csv file with accounts."""
    data = []
    f = open(file_name, 'r')
    for line in f:
        temp = tuple(line.strip().split(';'))
        data.append(temp)
    f.close()
    return data


def write_panda_to_excel(df, start_cell, wb):
    """Write Pandas DataFrame to Excel."""
    sht = wb.sheets['Input']
    sht.range(start_cell).value = df


def add_word_cloud(name, cell, wb):
    """Add the WordCloud to Sheet2 """
    sht = wb.sheets['Sheet2']
    name = os.getcwd() + '\\' + name
    rng = sht.range(cell)
    sht.pictures.add(name, top=rng.top, left=rng.left, width=325, height=155)


def add_profile_picture(user, cell, wb):
    #Add charts to dashboard.
    sht = wb.sheets['Sheet1']
    picture = [f for f in os.listdir('.') if f.startswith(user + '.')][0]
    name = os.getcwd() + '\\' + picture
    rng = sht.range(cell)
    sht.pictures.add(name, top=rng.top, left=rng.left, width=70, height=90)


app = xw.App(visible=False)

# Read users
os.chdir('../FolderA/')
file_name = 'accounts_file.csv'
users = get_users(file_name)
os.chdir('../Data')

for i, user in enumerate(users):
    try:
        #count += 1
        print(100 * '-')
        print(len(users), i+1, user)

        # go to directory where the dashboard is stored
        os.chdir('../Folder5/FolderE')
        wb = xw.Book('Twitter - Individuele Rapportage.xlsm')
        os.chdir('../../Data/' + user)

        # Remove file if exists
        xl = [e for e in os.listdir('.') if e.endswith('.xlsm')]
        for e in xl:
            os.remove(e)

        # add user name to title of Dashboard
        sht = wb.sheets['Input_Data']

        # add the csv data and profile pictures the other data to the dashboard
        df = pd.read_csv(user + '_header_info.csv', sep=',')
        write_panda_to_excel(df, 'A1', wb)

        cell = 'L20'
        try:
            add_profile_picture(user, cell, wb)
        except:
            os.chdir('../../Folder6')
            with open('Twitter - Profile picture Error.txt', 'a') as ExceptFile:
                ExceptFile.write(str(user) + '\n')
                os.chdir('../Data/' + user)

        name = user + '_WC.png'
        cell = 'Y46'
        add_word_cloud(name, cell, wb)

        xlname = 'Twitter' + user + '.xlsm'

        try:
            wb.save(xlname)
            wb.close()

        except:
            os.chdir('../../Folder6')
            with open('Twitter - Dashboard Generation Errors.txt', 'a') as myfile:
                myfile.write(str(user + "\n"))
                myfile.close()
            os.chdir('../Data/' + user)
        os.chdir('..')

    except OSError as exception:
        print(exception)
        os.chdir('..')
        with open('dash_errors.txt', 'w') as dashboard_errors:
            dashboard_errors.write(user+"\n")

我编辑了这个问题。也许现在更清楚了