我的代码 # 将随机生成的数据写入 Excel 文件中 for n, sheet in enumerate(rb.sheets()): start_row = 14 # 修改起始行数 for row_data in rows: start_col = 2 # 修改起始列数 for col_idx, cell_data in enumerate(row_data): style = style_list[sheet.cell_xf_index(start_row - 1, start_col - 1)] # 在身份证号码后提取需要解析的部分 if col_idx == 3: worksheet.write(start_row, start_col, xlwt.Formula( '=IF(ISERROR(IF(LEN(E{0})=18,DATE(MIDB(E{0},7,4),MIDB(E{0},11,2),MIDB(E{0},13,2)),DATE(MIDB(E{0},7,2),MIDB(E{0},9,2),MIDB(E{0},11,2)))),"",IF(LEN(E{0})=18,DATE(MIDB(E{0},7,4),MIDB(E{0},11,2),MIDB(E{0},13,2)),DATE(MIDB(E{0},7,2),MIDB(E{0},9,2),MIDB(E{0},11,2)))'.format( start_row + 1)), style) elif col_idx == 4: cell_data = f'=IF(RIGHT(E{start_row + 1},1)%2=0,"女","男")' worksheet.write(start_row, start_col, cell_data, style) worksheet.write(start_row, start_col, cell_data, style) # 修改写入位置 # # 第六列数据格式设置日期 # if start_col == 5: # worksheet.write(start_row, start_col, datetime.strptime(cell_data, "%Y/%m/%d")) start_col += 1 start_row += 1
时间: 2024-04-23 08:23:15 浏览: 21
这段代码是用于将随机生成的数据写入 Excel 文件中的。首先,使用 `enumerate` 函数遍历 Excel 文件中的所有表格。然后,使用嵌套的循环遍历每个表格中的每一行和每一列,将生成的数据写入 Excel 文件中的相应单元格中。在写入某些单元格时,使用了 Excel 公式来计算数据,例如在第 3 列中提取身份证号码并计算出生日期,以及在第 4 列中根据身份证号码判断性别。
需要注意的是,这段代码使用了 `xlwt` 库来写入 Excel 文件,而 `xlwt` 库是用于写入 Excel 2003 格式的。如果您需要写入 Excel 2007 及以上版本的文件,可以考虑使用 `openpyxl` 或 `xlsxwriter` 等库。
相关问题
# 创建工作簿和工作表 workbook = openpyxl.Workbook() sheet = workbook.active # 从文本框和下拉框中获取数据,并按列写入Excel文件 rows = self.textbox.get("1.0", "end").split("\n") for row_index, row in enumerate(rows): if not row.strip(): continue cells = row.split("\t") for cell_index, cell in enumerate(cells): if cell_index > sheet.max_column - 1: sheet.cell(row=row_index + 1, column=cell_index + 1, value=cell) else: sheet.cell(row=row_index + 1, column=cell_index + 1).value = cell for widget_index, widget in enumerate(self.textbox.winfo_children()): if isinstance(widget, tk.ttk.Combobox) and widget_index == row_index: sheet.cell(row=row_index + 1, column=len(cells) + 1, value=widget.get()) # 保存Excel文件 workbook.save(r"D:\點檢系統存放資料夾\點檢明細\1.xlsx")修改這段代碼中如果沒有日志框數據就跳過寫入
可以在循环内部添加一个判断,如果当前行为空行,则跳过写入Excel文件的操作。修改后的代码如下:
```
workbook = openpyxl.Workbook()
sheet = workbook.active
rows = self.textbox.get("1.0", "end").split("\n")
for row_index, row in enumerate(rows):
if not row.strip():
continue # 当前行为空行,跳过写入Excel文件的操作
cells = row.split("\t")
for cell_index, cell in enumerate(cells):
if cell_index > sheet.max_column - 1:
sheet.cell(row=row_index + 1, column=cell_index + 1, value=cell)
else:
sheet.cell(row=row_index + 1, column=cell_index + 1).value = cell
for widget_index, widget in enumerate(self.textbox.winfo_children()):
if isinstance(widget, tk.ttk.Combobox) and widget_index == row_index:
sheet.cell(row=row_index + 1, column=len(cells) + 1, value=widget.get())
workbook.save(r"D:\點檢系統存放資料夾\點檢明細\1.xlsx")
```
写入时,从第二列开始:import random # 生成随机数据 data = [] for i in range(10): row = [i+1, random.choice(['男', '女']), random.choice(['教师', '学生', '职员']), random.randint(1, 10), random.randint(10000000000, 99999999999)] data.append(row) # 写入Excel表格 for row_idx, row_data in enumerate(data): for col_idx, cell_data in enumerate(row_data): sheet.write(row_idx+1, col_idx, cell_data)
如果您要从第二列开始写入数据,只需在内部循环中将列索引加1即可。以下是修改后的代码示例:
```python
import random
# 生成随机数据
data = []
for i in range(10):
row = [i+1, random.choice(['男', '女']), random.choice(['教师', '学生', '职员']), random.randint(1, 10), random.randint(10000000000, 99999999999)]
data.append(row)
# 写入Excel表格
for row_idx, row_data in enumerate(data):
for col_idx, cell_data in enumerate(row_data):
sheet.write(row_idx+1, col_idx+1, cell_data)
```
在这个例子中,我们只需将内部循环中的`col_idx`加一即可。这样,我们将从第二列开始写入数据。请注意,在循环中,我们使用`row_idx+1`将数据写入第二行及以下行,因为第一行已经包含了您的列名。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)