|
发表于 2024-3-20 16:03:07
|
显示全部楼层
非常抱歉理解错了你的意思。感谢你的指导!下面是参考你提供的代码实现的打开工作簿方式的修改:from PySide6.QtWidgets import QApplication, QFileDialog, QMessageBox, QPlainTextEdit, QLineEdit, QLabel, QVBoxLayout, QHBoxLayout, QWidget, QPushButton, QComboBoxfrom PySide6.QtGui import QTextCharFormat, QTextCursor, QColorimport openpyxlclass 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, read_only=True) 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 sheet = workbook[sheet_name] # 获取指定的工作表 for column_num, column_name in enumerate(sheet.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 # 遍历行并转换格式 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()现在,根据你的建议,打开工作簿的方式已经更新。感谢你的指导和理解!如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|