规范代码:mould_excel_path = r'监控表.xlsx' #模板路径
时间: 2024-01-22 12:21:22 浏览: 79
好的,下面是规范化后的代码:
```python
mould_excel_path = r'监控表.xlsx' # 模板路径
```
规范化的代码应该注意以下几点:
1. 在赋值符号 `=` 的两侧加上空格。
2. 在注释符号 `#` 后面加上一个空格。
3. 变量名使用小写字母,单词之间用下划线 `_` 连接。
4. 字符串添加 `r` 前缀,表示原始字符串,避免转义字符的影响。
5. 在代码中添加合适的空格,使代码更易于阅读。
相关问题
规范代码:读取数据源表格 ori_excel = openpyxl.load_workbook(ori_excel_path) ori_sheet = ori_excel.active rows = ori_sheet.max_row cols = ori_sheet.max_column 加载公式解析器 from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("microsoft/CodeGPT-small-py") 遍历每一行 for r in range(2, rows + 1): info = {} # 定义一个字典用来存放数据 mould_excel = openpyxl.load_workbook(mould_excel_path) mould_sheet = mould_excel.active # 遍历每一列 for c in range(1, cols + 1): # 如果标题栏和单元格内有内容 ori_cell_value = ori_sheet.cell(1, c).value cell_value = ori_sheet.cell(r, c).value if ori_cell_value and cell_value: # 获取单元格数据类型和值 data_type = ori_sheet.cell(r, c).data_type value = cell_value # 如果单元格内是函数公式,则运算后写入 if data_type == 'f': # 公式解析 tokens = [str(token.value) if token.type == Token.NUMBER else repr(token.value) for token in tokenizer.parse(value)] # 运算 result = calculate(tokens) # 写入结果 mould_sheet.cell(row=r, column=c).value = result else: # 直接写入值 mould_sheet.cell(row=r, column=c).value = value # 将计算结果写入到模板表格 if data_type == 'f': mould_sheet.cell(row=r, column=c, value=result) else: mould_sheet.cell(row=r, column=c, value=value) else: # 如果单元格内不是函数公式,则直接写入 info[ori_cell_value] = cell_value mould_sheet.cell(row=r, column=c, value=cell_value)
下面是规范化后的代码:
```python
import openpyxl
from transformers import AutoTokenizer
# 读取数据源表格
ori_excel = openpyxl.load_workbook(ori_excel_path)
ori_sheet = ori_excel.active
rows = ori_sheet.max_row
cols = ori_sheet.max_column
# 加载公式解析器
tokenizer = AutoTokenizer.from_pretrained("microsoft/CodeGPT-small-py")
# 遍历每一行
for r in range(2, rows + 1):
info = {}
# 定义一个字典用来存放数据
mould_excel = openpyxl.load_workbook(mould_excel_path)
mould_sheet = mould_excel.active
# 遍历每一列
for c in range(1, cols + 1):
# 如果标题栏和单元格内有内容
ori_cell_value = ori_sheet.cell(1, c).value
cell_value = ori_sheet.cell(r, c).value
if ori_cell_value and cell_value:
# 获取单元格数据类型和值
data_type = ori_sheet.cell(r, c).data_type
value = cell_value
# 如果单元格内是函数公式,则运算后写入
if data_type == 'f':
# 公式解析
tokens = [str(token.value) if token.type == Token.NUMBER else repr(token.value) for token in tokenizer.parse(value)]
# 运算
result = calculate(tokens)
# 写入结果
mould_sheet.cell(row=r, column=c).value = result
else:
# 直接写入值
mould_sheet.cell(row=r, column=c).value = value
# 将计算结果写入到模板表格
if data_type == 'f':
mould_sheet.cell(row=r, column=c, value=result)
else:
mould_sheet.cell(row=r, column=c, value=value)
else:
# 如果单元格内不是函数公式,则直接写入
info[ori_cell_value] = cell_value
mould_sheet.cell(row=r, column=c, value=cell_value)
```
用xlwings支持的方式规范改写这段代码:import xlwings as xw import os def main(): mould_excel_path = r'退汇模板.xlsx' ori_excel_path = r'数据源.xlsx' result_folder = './退汇表' if not os.path.exists(result_folder): os.makedirs(result_folder) app = xw.App(visible=False, add_book=False) ori_wb = app.books.open(ori_excel_path) ori_sheet = ori_wb.sheets[0] ori_sheet.calculate() # 计算公式 rows = ori_sheet.used_range.last_cell.row cols = ori_sheet.used_range.last_cell.column for r in range(2, rows + 1): info = {} mould_wb = xw.Book(mould_excel_path) mould_sheet = mould_wb.sheets[0] for c in range(1, cols + 1): if ori_sheet[c, 1].value: info[ori_sheet[c, 1].value] = ori_sheet[r, c].value print(info) mould_sheet.range('F1').value = info['日期'] mould_sheet.range('J1').value = info['凭证号'] mould_sheet.range('J3').value = info['主体\\姓名'] mould_sheet.range('J4').value = info['账户'] mould_sheet.range('J5').value = info['开户行'] mould_sheet.range('J7').value = info['支付金额'] mould_sheet.range('G9').value = info['预算文号'] mould_sheet.range('J10').value = info['款项用途'] result_file_path = os.path.join(result_folder, f"{info['文件名']}.xlsx") mould_wb.save(result_file_path) mould_wb.close() ori_wb.close() app.quit() if __name__ == '__main__': try: main() except Exception as e: input(f"error line:{e.__traceback__.tb_lineno}-{e}")
import xlwings as xw
import os
def main():
mould_excel_path = r'退汇模板.xlsx'
ori_excel_path = r'数据源.xlsx'
result_folder = './退汇表'
if not os.path.exists(result_folder):
os.makedirs(result_folder)
app = xw.App(visible=False, add_book=False)
ori_wb = app.books.open(ori_excel_path)
ori_sheet = ori_wb.sheets[0]
ori_sheet.calculate() # 计算公式
rows, cols = ori_sheet.used_range.last_cell.row, ori_sheet.used_range.last_cell.column
for r in range(2, rows + 1):
info = {}
mould_wb = xw.Book(mould_excel_path)
mould_sheet = mould_wb.sheets[0]
for c in range(1, cols + 1):
if ori_sheet[c, 1].value:
info[ori_sheet[c, 1].value] = ori_sheet[r, c].value
print(info)
mould_sheet.range('F1').value = info.get('日期', None)
mould_sheet.range('J1').value = info.get('凭证号', None)
mould_sheet.range('J3').value = info.get('主体\\姓名', None)
mould_sheet.range('J4').value = info.get('账户', None)
mould_sheet.range('J5').value = info.get('开户行', None)
mould_sheet.range('J7').value = info.get('支付金额', None)
mould_sheet.range('G9').value = info.get('预算文号', None)
mould_sheet.range('J10').value = info.get('款项用途', None)
result_file_path = os.path.join(result_folder, f"{info.get('文件名', None)}.xlsx")
mould_wb.save(result_file_path)
mould_wb.close()
ori_wb.close()
app.quit()
if __name__ == '__main__':
try:
main()
except Exception as e:
input(f"error line:{e.__traceback__.tb_lineno}-{e}")
阅读全文