|
发表于 2024-3-22 17:18:15
|
显示全部楼层
下面是一个满足您需求的示例代码:
- import sys
- from PySide6.QtWidgets import QApplication, QMainWindow, QLabel, QFileDialog, QPushButton, QComboBox
- from PySide6.QtCore import Qt
- import openpyxl as xl
- class ExcelConverter(QMainWindow):
- def __init__(self):
- super().__init__()
- self.setWindowTitle("Excel格式转换")
- self.setGeometry(300, 300, 400, 200)
- self.file_path = ""
- self.sheet_name = ""
- self.column_name = ""
- self.cell_format = ""
- self.label = QLabel(self)
- self.label.setText("请选择要转换的工作簿:")
- self.label.move(20, 20)
- self.select_button = QPushButton(self)
- self.select_button.setText("选择文件")
- self.select_button.move(200, 15)
- self.select_button.clicked.connect(self.select_file)
- self.sheet_label = QLabel(self)
- self.sheet_label.setText("请选择要转换的工作表:")
- self.sheet_label.move(20, 60)
- self.sheet_combo = QComboBox(self)
- self.sheet_combo.move(200, 55)
- self.column_label = QLabel(self)
- self.column_label.setText("请选择要转换的列:")
- self.column_label.move(20, 100)
- self.column_combo = QComboBox(self)
- self.column_combo.move(200, 95)
- self.format_label = QLabel(self)
- self.format_label.setText("请选择转换后的格式:")
- self.format_label.move(20, 140)
- self.format_combo = QComboBox(self)
- self.format_combo.addItem("常规")
- self.format_combo.addItem("数值")
- self.format_combo.addItem("文本")
- self.format_combo.addItem("日期")
- self.format_combo.move(200, 135)
- self.convert_button = QPushButton(self)
- self.convert_button.setText("开始转换")
- self.convert_button.move(200, 175)
- self.convert_button.clicked.connect(self.convert)
- def select_file(self):
- file_dialog = QFileDialog()
- self.file_path, _ = file_dialog.getOpenFileName(self, "选择文件", "", "Excel Files (*.xls *.xlsx)")
- if self.file_path:
- self.load_sheets()
- def load_sheets(self):
- wb = xl.load_workbook(self.file_path)
- sheet_names = wb.sheetnames
- self.sheet_combo.clear()
- self.sheet_combo.addItems(sheet_names)
- def convert(self):
- if not self.file_path:
- return
- self.sheet_name = self.sheet_combo.currentText()
- self.column_name = self.column_combo.currentText()
- self.cell_format = self.format_combo.currentText()
- wb = xl.load_workbook(self.file_path)
- sheet = wb[self.sheet_name]
- column_index = None
- for row in sheet.iter_rows(min_row=1, max_row=1):
- for cell in row:
- if cell.value == self.column_name:
- column_index = cell.column
- break
- if column_index:
- break
- if column_index:
- for row in sheet.iter_rows(min_row=2, values_only=True):
- cell = sheet[f"{column_index}{row[0]}"]
- converted_value = self.convert_value(cell.value, self.cell_format)
- sheet[f"{chr(ord(column_index) + 1)}{row[0]}"] = converted_value
- wb.save(self.file_path)
- self.column_combo.clear()
- self.load_columns()
- print("转换成功!")
- else:
- print("找不到指定的列名!")
- @staticmethod
- def convert_value(value, cell_format):
- if cell_format == "常规":
- return value
- elif cell_format == "数值":
- try:
- return float(value)
- except ValueError:
- return None
- elif cell_format == "文本":
- return str(value)
- elif cell_format == "日期":
- try:
- return value.strftime("%Y-%m-%d")
- except AttributeError:
- return None
- return None
- def load_columns(self):
- wb = xl.load_workbook(self.file_path)
- sheet = wb[self.sheet_name]
- column_names = []
- for row in sheet.iter_rows(min_row=1, max_row=1, values_only=True):
- column_names = [str(cell) for cell in row]
- break
- self.column_combo.clear()
- self.column_combo.addItems(column_names)
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- converter = ExcelConverter()
- converter.show()
- sys.exit(app.exec())
复制代码
在这个示例中,使用了PySide6创建了一个简单的GUI界面。用户可以点击“选择文件”按钮来选择要转换的工作簿,然后程序会自动加载工作簿中的所有工作表名称。用户可以从下拉框中选择要转换的工作表。
接着,用户可以选择要转换的列,并指定转换后的单元格格式。点击“开始转换”按钮后,程序会读取工作表中指定列的数据,并根据用户选择的单元格格式进行转换。转换后的数据会写入工作表的“转换后列”中。
需要注意的是,身份证号码一般是文本型数据,转换为数值型时要关闭科学计数法,避免最后四位变为0。示例代码中的`convert_value`函数对数值类型进行了处理。如果没有找到指定的列名,会提示用户找不到列名。
转换成功后,程序会重新加载可供选择的列名,以便用户继续转换其他列。
请注意安装好所需的库(PySide6和openpyxl),并使用Python运行该代码。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|