【问题标题】:how to handle excel file (xlsx,xls) with excel formulas(macros) in python如何在python中使用excel公式(宏)处理excel文件(xlsx,xls)
【发布时间】:2018-08-10 14:40:35
【问题描述】:

我需要在迭代中将 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')

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

input 1 input 2 input 3

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

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

【问题讨论】:

    标签: python excel python-module


    【解决方案1】:

    幸运的是,我发现下面的代码可以获取 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?以其他方式使用信息

    【讨论】:

      猜你喜欢
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-11
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      相关资源
      最近更新 更多