Here's a Python code example that demonstrates how to read from and write to an XLSX file using the openpyxl library:

import openpyxl

# Open the existing XLSX file
workbook = openpyxl.load_workbook('example.xlsx')

# Select a specific sheet within the workbook
sheet = workbook['Sheet1']

# Read data from the sheet
cell_value = sheet['A1'].value
print(cell_value)

# Write data to the sheet
sheet['B1'] = 'New value'

# Save changes to the XLSX file
workbook.save('example.xlsx')

This code reads an existing XLSX file named 'example.xlsx', selects the first sheet ('Sheet1'), reads the value from cell 'A1', prints it to the console, writes a new value to cell 'B1', and saves the changes back to the same XLSX file.

Note that you'll need to have the 'openpyxl' library installed in your Python environment in order to run this code successfully. You can install it using pip by running 'pip install openpyxl' in your terminal or command prompt.

Python XLSX File Read and Write with openpyxl

原文地址: https://www.cveoy.top/t/topic/lFAC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录