当前位置:   article > 正文

python读取excel文件中的所有Sheet的所有单元格函数,并把结果写到文件_python遍历所有sheet单元格位置

python遍历所有sheet单元格位置

要读取一个XLSX文件中的所有Sheet的所有单元格函数,您可以使用Python的openpyxl库。首先,确保您已经安装了openpyxl库,如果没有安装,可以使用以下命令进行安装:

pip install openpyxl
  • 1

然后,您可以使用以下Python代码来读取XLSX文件中的函数:

import openpyxl

# 打开XLSX文件
file_path = 'your_xlsx_file.xlsx'
workbook = openpyxl.load_workbook(file_path)

# 打开文本文件以写入模式
output_file = open('output.txt', 'w')

# 遍历所有Sheet
for sheet_name in workbook.sheetnames:
    sheet = workbook[sheet_name]
    output_file.write(f"Sheet Name: {sheet_name}\n")

    # 遍历每一行
    for row in sheet.iter_rows():
        # 遍历每个单元格
        for cell in row:
            if cell.data_type == 'f' and cell.value is not None:
                output_file.write(f"Cell Formula: {cell.value}\n")

# 关闭文本文件
output_file.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

这段代码会打开XLSX文件,然后遍历所有Sheet,逐行逐单元格查找包含函数的单元格,并将其公式写入名为’output.txt’的文本文件中。请将your_xlsx_file.xlsx替换为您要处理的XLSX文件的路径。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号