如何使用从 xlrd 读取的格式并在 python 中通过 xlsxwriter 写入 [英] how to use format read from xlrd and write thru xlsxwriter in python

查看:34
本文介绍了如何使用从 xlrd 读取的格式并在 python 中通过 xlsxwriter 写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 xlrd 读取 excel 文件.做一些宏替换,然后通过 xlsxwriter 写入.无需阅读和复制格式信息,代码就可以工作,但是当我添加格式信息时,出现错误(在底部)代码片段如下..我读取了一个 xls 文件,对于每个数据行,我用值替换令牌宏并写回.当我尝试关闭 output_workbook 时出现错误

I am reading an excel file using xlrd. Doing some macro replacing and then writing thru xlsxwriter. Without reading and copying formatting info the code works but when I add formatting info I get an error (at the bottom) The code snippet is below..I read a xls file, for each data row I replace token macros with the values and write back. when I try to close the output_workbook I get an error

    filePath = os.path.realpath(os.path.join(inputPath,filename))
    input_workbook = open_workbook(filePath, formatting_info=True)
    input_DataSheet = input_workbook.sheet_by_index(0)
    data = [[input_DataSheet.cell_value(r,c) for c in range(input_DataSheet.ncols)] for r in range(input_DataSheet.nrows)]

    output_workbook = xlsxwriter.Workbook('C:\Users\Manish\Downloads\Sunny\Drexel_Funding\MacroReplacer\demo.xlsx')
    output_worksheet = output_workbook.add_worksheet()

    for rowIndex, value in enumerate(data):
        copyItem = []

        for individualItem in value:
            tempItem = individualItem

            if (isinstance(individualItem, basestring)):
                tempItem = tempItem.replace("[{0}]".format(investorNameMacro), investorName)
                tempItem = tempItem.replace("[{0}]".format(investorPhoneMacro), investorPhone)
                tempItem = tempItem.replace("[{0}]".format(investorEmailMacro), investorEmail)
                tempItem = tempItem.replace("[{0}]".format(loanNumberMacro), loanNumber)

            copyItem.append(tempItem)

        for columnIndex, val in enumerate(copyItem):
            fmt =input_workbook.xf_list[input_DataSheet.cell(rowIndex,columnIndex).xf_index]
            output_worksheet.write(rowIndex,columnIndex, val,fmt)

    output_workbook.close()

我得到的错误是

回溯(最近一次调用最后一次):文件C:/Users/Manish/Downloads/Sunny/Drexel_Funding/MacroReplacer/drexelfundingmacroreplacer.py",第 87 行,在output_workbook.close()文件build\bdist.win-amd64\egg\xlsxwriter\workbook.py",第 297 行,关闭_store_workbook 中的文件build\bdist.win-amd64\egg\xlsxwriter\workbook.py",第 605 行文件build\bdist.win-amd64\egg\xlsxwriter\packager.py",第 131 行,在 _create_package 中文件build\bdist.win-amd64\egg\xlsxwriter\packager.py",第 189 行,在 _write_worksheet_files文件build\bdist.win-amd64\egg\xlsxwriter\worksheet.py",第 3426 行,在 _assemble_xml_file文件build\bdist.win-amd64\egg\xlsxwriter\worksheet.py",第 4829 行,在 _write_sheet_data 中文件build\bdist.win-amd64\egg\xlsxwriter\worksheet.py",第 5015 行,在 _write_rows文件build\bdist.win-amd64\egg\xlsxwriter\worksheet.py",第 5183 行,在 _write_cell 中AttributeError: 'XF' 对象没有属性 '_get_xf_index'

Traceback (most recent call last): File "C:/Users/Manish/Downloads/Sunny/Drexel_Funding/MacroReplacer/drexelfundingmacroreplacer.py", line 87, in output_workbook.close() File "build\bdist.win-amd64\egg\xlsxwriter\workbook.py", line 297, in close File "build\bdist.win-amd64\egg\xlsxwriter\workbook.py", line 605, in _store_workbook File "build\bdist.win-amd64\egg\xlsxwriter\packager.py", line 131, in _create_package File "build\bdist.win-amd64\egg\xlsxwriter\packager.py", line 189, in _write_worksheet_files File "build\bdist.win-amd64\egg\xlsxwriter\worksheet.py", line 3426, in _assemble_xml_file File "build\bdist.win-amd64\egg\xlsxwriter\worksheet.py", line 4829, in _write_sheet_data File "build\bdist.win-amd64\egg\xlsxwriter\worksheet.py", line 5015, in _write_rows File "build\bdist.win-amd64\egg\xlsxwriter\worksheet.py", line 5183, in _write_cell AttributeError: 'XF' object has no attribute '_get_xf_index'

感谢任何帮助

谢谢

推荐答案

Xlrd 和 XlsxWriter 格式是不同的对象类型,不可互换.

The Xlrd and XlsxWriter formats are different object types and are not interchangeable.

如果您希望保留格式,则必须编写一些代码将属性从一种转换为另一种.

If you wish to preserve formatting you will have to write some code that translates the properties from one to the other.

这篇关于如何使用从 xlrd 读取的格式并在 python 中通过 xlsxwriter 写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆