我需要在迭代中将Input_data.xls的输入传递到现有的xls文件,该文件在使用python3.6的各个单元中具有特殊功能。这些功能根据输入更改现有xls中的主要数据。但是,当xlrd打开文件时,它不会导入xls单元功能并保存修改后的文件。并写下对象名称而不是其值

Python代码:

import xlrd
import xlwt
import xlutils
from xlrd import open_workbook
from xlutils.copy import copy
import os.path

book = xlrd.open_workbook('input_data.xlsx')
sheet0 = book.sheet_by_index(0)
for i in range (sheet0.nrows):
    st1=sheet0.row_values(i+1)
    TIP=[st1[0]]
    OOIPAN_IP=[st1[1]]
    NM=[st1[2]]
    book1 = xlrd.open_workbook('primary_data.xls')
    wb=copy(book1)
    w_sheet=wb.get_sheet(0)
    w_sheet.write(1,0,'TIP')
    w_sheet.write(1,1,'OIP')
    w_sheet.write(1,2,'NM')
    wb.save('ipsectemp.xls')


在单元格中写入对象名称,而不是对象的vlaue

input 1 input 2 input 3

st1[0]  st1[1]  st1[2]


哪个模块可以使用python中的excel函数(宏)来帮助打开/读取/写入工作簿。

最佳答案

幸运的是,我发现下面的代码可以获取excel宏,openpyxl模块使用单元格值可以很好地工作

    book = load_workbook('primary_data.xlsx') #open ipsec file with desired inputs
    sheet0 = book.get_sheet_by_name('Sheet1')
    for row in range(2,sheet0.max_row+1):
        for column in "A":  #Here add or reduce the columns
            cell_name = "{}{}".format(column, row)
            textlt=sheet0[cell_name].value
            print(textlt)


从此答案中提取的信息
openpyxl - read only one column from excel file in python?以其他方式使用信息

关于python - 如何在python中使用excel公式(宏)处理excel文件(xlsx,xls),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51788896/

10-14 17:59
查看更多