明白了你的要求,下面是根据你的需求修改后的代码示例:
from PySide6.QtWidgets import QApplication, QFileDialog, QMessageBox, QPlainTextEdit, QLineEdit, QLabel, QVBoxLayout, QHBoxLayout, QWidget, QPushButton, QComboBox
from PySide6.QtGui import QTextCharFormat, QTextCursor, QColor
import xlrd
import openpyxl
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Excel文件内容转换')
self.resize(500, 300)
self.layout = QVBoxLayout()
# 文件路径选择按钮和标签
self.file_path_btn = QPushButton('选择文件路径')
self.file_path_label = QLabel('文件路径:')
# 工作簿选择按钮和标签
self.workbook_btn = QPushButton('选择工作簿')
self.workbook_label = QLabel('工作簿:')
# 转换列选择框
self.source_col_label = QLabel('源列:')
self.source_col_combo = QComboBox()
self.dest_col_label = QLabel('目标列:')
self.dest_col_combo = QComboBox()
# 转换按钮
self.convert_btn = QPushButton('转换')
# 添加控件到布局中
self.layout.addWidget(self.file_path_btn)
self.layout.addWidget(self.file_path_label)
self.layout.addWidget(self.workbook_btn)
self.layout.addWidget(self.workbook_label)
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.convert_btn)
self.setLayout(self.layout)
# 绑定按钮点击事件
self.file_path_btn.clicked.connect(self.select_file_path)
self.workbook_btn.clicked.connect(self.select_workbook)
self.convert_btn.clicked.connect(self.convert)
def select_file_path(self):
file_dialog = QFileDialog()
file_path = file_dialog.getOpenFileName(self, '选择文件')[0]
self.file_path_label.setText(f'文件路径: {file_path}')
def select_workbook(self):
file_dialog = QFileDialog()
workbook_path = file_dialog.getOpenFileName(self, '选择工作簿')[0]
self.workbook_label.setText(f'工作簿: {workbook_path}')
def convert(self):
file_path = self.file_path_label.text().split(': ')[1]
workbook_name = self.workbook_label.text().split(': ')[1]
source_col = self.source_col_combo.currentText()
dest_col = self.dest_col_combo.currentText()
# 打开工作簿
try:
workbook = self.open_workbook(file_path, workbook_name)
except Exception as e:
QMessageBox.warning(self, '错误', f'打开工作簿失败:{str(e)}')
return
# 获取源列和目标列的索引
try:
source_col_index = self.get_column_index(source_col)
dest_col_index = self.get_column_index(dest_col)
except ValueError as e:
QMessageBox.warning(self, '错误', str(e))
return
# 获取活动的工作表
worksheet = workbook.active
# 遍历行并转换格式
for row in worksheet.iter_rows(min_row=2, values_only=True):
source_value = row[source_col_index - 1]
# 执行转换操作,这里只是一个示例,你可以根据需要进行修改
dest_value = str(source_value).upper()
# 将转换结果写入目标列
row[dest_col_index - 1] = dest_value
# 保存转换后的文件
try:
self.save_workbook(workbook, file_path)
except Exception as e:
QMessageBox.warning(self, '错误', f'保存工作簿失败:{str(e)}')
return
# 清空下拉框选择
self.source_col_combo.clear()
self.dest_col_combo.clear()
# 询问用户是否继续转换
reply = QMessageBox.question(
self, '询问', '是否继续转换?', QMessageBox.Yes | QMessageBox.No
)
if reply == QMessageBox.No:
QApplication.quit()
def open_workbook(self, file_path, workbook_name):
if file_path.endswith('.xls'):
workbook = xlrd.open_workbook(file_path)
elif file_path.endswith('.xlsx'):
workbook = openpyxl.load_workbook(file_path)
else:
raise ValueError('不支持的文件格式')
if workbook_name:
workbook.active = workbook
return workbook
def get_column_index(self, column_name):
if not column_name.isalpha():
raise ValueError('列名必须由字母组成')
index = 0
for char in column_name:
index = index * 26 + ord(char.upper()) - ord('A') + 1
return index
def save_workbook(self, workbook, file_path):
if file_path.endswith('.xls'):
workbook.save(file_path)
elif file_path.endswith('.xlsx'):
workbook.save(file_path)
else:
raise ValueError('不支持的文件格式')
app = QApplication()
window = MainWindow()
window.show()
app.exec()
在这个代码示例中,我添加了选择文件路径和工作簿的按钮,使用`QFileDialog`选择文件路径和工作簿。用户可以通过点击这两个按钮选择对应的路径和文件。并将选择路径和工作簿的结果显示在对应的标签中。
同时,源列和目标列选择框使用`QComboBox`控件,并通过`currentText()`方法获取当前选择的列名。
希望这个修改满足了你的需求。如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |