非常感谢你提供的代码!根据你的代码,做了一些修改和补充,以满足你的需求。以下是更新后的代码:
from PySide6.QtWidgets import QApplication, QFileDialog, QMessageBox, QPlainTextEdit, QLineEdit, QLabel, QVBoxLayout, QHBoxLayout, QWidget, QPushButton, QComboBox
from PySide6.QtGui import QTextCharFormat, QTextCursor, QColor
import openpyxl
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Excel文件内容转换')
self.resize(500, 300)
self.layout = QVBoxLayout()
# 工作簿选择按钮和标签
self.workbook_btn = QPushButton('选择工作簿')
self.workbook_label = QLabel('工作簿:')
# 工作表选择框
self.sheet_label = QLabel('工作表:')
self.sheet_combo = QComboBox()
# 转换列选择框
self.source_col_label = QLabel('源列:')
self.source_col_combo = QComboBox()
self.dest_col_label = QLabel('目标列:')
self.dest_col_combo = QComboBox()
# 单元格格式选择框
self.cell_format_label = QLabel('单元格格式:')
self.cell_format_combo = QComboBox()
# 转换按钮
self.convert_btn = QPushButton('转换')
# 添加控件到布局中
self.layout.addWidget(self.workbook_btn)
self.layout.addWidget(self.workbook_label)
self.layout.addWidget(self.sheet_label)
self.layout.addWidget(self.sheet_combo)
self.layout.addWidget(self.source_col_label)
self.layout.addWidget(self.source_col_combo)
self.layout.addWidget(self.dest_col_label)
self.layout.addWidget(self.dest_col_combo)
self.layout.addWidget(self.cell_format_label)
self.layout.addWidget(self.cell_format_combo)
self.layout.addWidget(self.convert_btn)
self.setLayout(self.layout)
# 绑定按钮点击事件
self.workbook_btn.clicked.connect(self.select_workbook)
self.convert_btn.clicked.connect(self.convert)
self.cell_format_combo.currentIndexChanged.connect(self.handle_cell_format_changed)
def select_workbook(self):
file_dialog = QFileDialog()
workbook_path, _ = file_dialog.getOpenFileName(self, '选择工作簿')
self.workbook_label.setText(f'工作簿: {workbook_path}')
self.load_sheets(workbook_path)
def load_sheets(self, workbook_path):
if not workbook_path:
return
workbook = openpyxl.load_workbook(filename=workbook_path)
sheets = workbook.sheetnames
self.sheet_combo.clear()
self.sheet_combo.addItems(sheets)
# 加载列数据
sheet_name = self.sheet_combo.currentText()
self.load_columns(workbook_path, sheet_name)
def load_columns(self, workbook_path, sheet_name):
if not workbook_path or not sheet_name:
return
workbook = openpyxl.load_workbook(filename=workbook_path)
sheet = workbook[sheet_name] # 获取指定的工作表
column_names = [column.value for column in sheet.iter_cols(min_row=1, max_row=1)]
self.source_col_combo.clear()
self.dest_col_combo.clear()
for column_name in column_names:
self.source_col_combo.addItem(column_name)
self.dest_col_combo.addItem(column_name)
def handle_cell_format_changed(self, index):
cell_format = self.cell_format_combo.currentText()
def convert(self):
workbook_path = self.workbook_label.text().split(': ')[1]
sheet_name = self.sheet_combo.currentText()
source_col = self.source_col_combo.currentText()
dest_col = self.dest_col_combo.currentText()
cell_format = self.cell_format_combo.currentText()
# 打开工作簿
try:
workbook = openpyxl.load_workbook(filename=workbook_path)
except Exception as e:
QMessageBox.warning(self, '错误', f'打开工作簿失败:{str(e)}')
return
# 获取源列和目标列的索引
source_col_index = None
dest_col_index = None
for column_num, column_name in enumerate(worksheet.iter_cols(min_row=1, max_row=1, values_only=True), start=1):
if column_name == source_col:
source_col_index = column_num
if column_name == dest_col:
dest_col_index = column_num
if not source_col_index or not dest_col_index:
QMessageBox.warning(self, '错误', '未找到指定的源列或目标列')
return
# 获取活动的工作表
sheet = workbook[sheet_name]
# 遍历行并转换格式
for row in sheet.iter_rows(min_row=2, values_only=True):
source_value = row[source_col_index - 1]
# 执行转换操作,这里只是一个示例,你可以根据需要进行修改
dest_value = str(source_value).upper()
# 将转换结果写入目标列
sheet.cell(row=row.row, column=dest_col_index, value=dest_value)
# 设置单元格格式
cell = sheet.cell(row=row.row, column=dest_col_index)
cell.number_format = cell_format
# 保存转换后的文件
try:
workbook.save(filename=workbook_path)
except Exception as e:
QMessageBox.warning(self, '错误', f'保存工作簿失败:{str(e)}')
return
QMessageBox.information(self, '提示', '转换完成')
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
请注意,这个示例代码中使用了openpyxl库来操作Excel文件,根据用户的选择进行相应的操作。通过点击 "选择工作簿" 按钮,弹出文件选择对话框让用户选择要转换的工作簿。然后再根据用户的选择加载工作表和列数据。在转换过程中,你可以根据需要修改相关逻辑。希望这次能满足你的需求,如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |