gpt4 book ai didi

python - 如何在Python中使用Excel公式(宏)处理Excel文件(xlsx,xls)

转载 作者:行者123 更新时间:2023-12-01 09:09:25 24 4
gpt4 key购买 nike

我需要将迭代中的输入从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函数(宏)打开/读取/写入工作簿。

最佳答案

幸运的是,我发现下面的代码可以获取 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com