ori_fd = sys.argv[1] IndexError: list index out of range
时间: 2023-08-13 15:09:03 浏览: 47
这个错误意味着在使用sys.argv时,你没有提供足够的命令行参数。sys.argv是一个列表,其中包含了在命令行中传递给脚本的参数。索引为0的元素是脚本本身的名称,而其他索引对应于传递给脚本的参数。
在你的代码中,你尝试访问索引为1的元素,但是该索引超出了sys.argv列表的范围。这通常是由于在命令行中没有提供足够的参数导致的。
为了解决这个问题,你可以检查sys.argv列表的长度,确保至少有两个元素(脚本名称和至少一个参数),然后再访问索引为1的元素。或者,你可以在命令行中提供足够的参数来避免这个错误。
相关问题
用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}")
规范代码:读取数据源表格 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)
```