从 Excel 文件中提取特定值,将其放入静态文本并生成 .txt 文件

Extract particular values from Excel file, place it into static text and generate .txt files

我正在尝试从 .xls 文件中读取单元格并将它们放入静态文本中的特定位置,然后将其生成为 .txt 文件。

我设法编写了生成所需内容的代码,但它仅适用于 .xls 文件中的第一行。它根据行数生成 4 个文件,但每个文件都有第一行的值。我知道我在代码的第一行有静态单元格值,但我不确定如何修改它并让代码按单元格按 row/cell 行并为每一行生成相关的 .txt 文件。我的猜测是它需要以某种方式合并到 for 循环中?

对于上下文,每一行代表一台打印机,然后使用这些文件将打印机导入系统。

我会很高兴 help/suggestions。

这是我的测试 .xls 文件的样子

prnt    server    desc      shortnam    driver      type
-----------------------------------------------------------
deupr   fransf    pr deu    ds22        adasd2      bizhub
frapr   fransf    pr fra    ds23        asdasda3    hp
grcpr   fransf    pr grc    ds24        asdasdq5    testhub
sinpr   fransf    pr sin    ds25        qdsad8      testhp


这是我的代码状态(请原谅乱七八糟的 - 我是一个完全的初学者):

import xlrd


xlsfilename='Testing.xls'
test = xlrd.open_workbook(xlsfilename)
number_subjetcs=4 #will update this to require user input based on number of rows, test file has 4
number_columns=6 #is static

for row in range(number_subjetcs): 
    txtfilename = 'testfile' + str(row) + '.txt'
    with open(txtfilename, "w") as f:
        for row in range(1):            
            PrinterName = test.sheets()[0].cell(1,0).value
            ShortName = test.sheets()[0].cell(1,3).value
            Driver = test.sheets()[0].cell(1,4).value
            Model = test.sheets()[0].cell(1,5).value
            Desc = test.sheets()[0].cell(1,2).value
            Server = test.sheets()[0].cell(1,1).value
            f.write('VERSION = "0.1"')
            f.write('\n' + 'TIME = "20201015111814"')
            f.write('\n' + 'SYSTEM = "PR0"')
            f.write('\n' + 'RELEASE = "750"')
            f.write('\n' + '* DEVICE = {')
            f.write('\n' + '  NAME = "' + PrinterName + '"')
            f.write('\n' + '  PADEST = "' + ShortName + '"')
            f.write('\n' + '  PATYPE = "'+ Driver + '"')
            f.write('\n' + '  PAMODEL = "'+ Model + '"')
            f.write('\n' + '  PACLASS = ""')
            f.write('\n' + '  PAARCHIVER = ""')
            f.write('\n' + '  PALANGU = ""')
            f.write('\n' + '  PADISABLED = ""') #second file needs to have X here
            f.write('\n' + '  PANOQUERY = ""')
            f.write('\n' + '  PADUPCOPY = ""')
            f.write('\n' + '  PADUPLEX = ""')
            f.write('\n' + '  PASTORELOC = ""')
            f.write('\n' + '  PADFLTUTTL = ""')
            f.write('\n' + '  PADFLTSTTL = ""')
            f.write('\n' + '  PASYNC = ""')
            f.write('\n' + '  PAMONI = ""')
            f.write('\n' + '  PASTANDORT = "' + Desc + '"')
            f.write('\n' + '  PAMSG = "' + Desc + '"')
            f.write('\n' + '  PAMSSERVER = "SERVER_0"')
            f.write('\n' + '  PAMETHOD = "C"')
            f.write('\n' + '  PAPROSNAME = "\\' + Server + "\" + PrinterName + '"')
            f.write('\n' + '  PALOMS = ""')
            f.write('\n' + '  PALPRCMD = ""')
            f.write('\n' + '  PALPDHOST = ""')
            f.write('\n' + '  PALPDPORT = "0"')
            f.write('\n' + '  PACONNTIME = "0"')
            f.write('\n' + '  PAREADTIME = "0"')
            f.write('\n' + '  PATRAYS = "0"')
            f.write('\n' + '  PAXSHIFT = "0"')
            f.write('\n' + '  PAYSHIFT = "0"')
            f.write('\n' + '  PAXSHUNIT = "MM"')
            f.write('\n' + '  PAYSHUNIT = "MM"')
            f.write('\n' + '  PACRYPTMOD = ""')
            f.write('\n' + '  PACRYPTMET = ""')
            f.write('\n' + '  PANOVSERVR = ""')
            f.write('\n' + '  PAPOOLART = "P"')
            f.write('\n' + '  PATRACE2 = ""')
            f.write('\n' + '  PATRACEF = ""')
            f.write('\n' + '  PAPROTDATA = ""')
            f.write('\n' + '  PAPROTCMD = ""')
            f.write('\n' + '  PAPROTRES = ""')
            f.write('\n' + '  PAKEEPFILE = ""')
            f.write('\n' + '  CHGNAME1 = ""')
            f.write('\n' + '  CHGSAPREL1 = "750"')
            f.write('\n' + '  CHGSAPSYS1 = "PR0"')
            f.write('\n' + '  PADEVGRP = ""')
            f.write('\n' + '  COLORPRT = "X"')
            f.write('\n' + '  PRINTMODE = ""')
            f.write('\n' + '  INPUTTRAY = ""')
            f.write('\n' + '  PANOCHOICE = ""')
            f.write('\n' + '}')

在使用您的示例数据创建 Excel 文件后,以下代码在我的计算机上运行良好。这是我为使其工作而进行的更改:第二个 for 循环不是必需的,因此我将其删除并缩进了以前在该循环中的代码。我将 range() 调用更改为从 1 开始,然后超过行数,因为行号在 xlrd 中从零开始。我更改了 .cell 方法调用以使用 row 变量,而不是总是从第一行获取值。

import xlrd


xlsfilename='Testing.xls'
test = xlrd.open_workbook(xlsfilename)
number_subjetcs=4 #will update this to require user input based on number of rows, test file has 4
number_columns=6 #is static

for row in range(1, number_subjetcs + 1): 
    txtfilename = 'testfile' + str(row - 1) + '.txt'
    with open(txtfilename, "w") as f:
        PrinterName = test.sheets()[0].cell(row, 0).value
        ShortName = test.sheets()[0].cell(row, 3).value
        Driver = test.sheets()[0].cell(row, 4).value
        Model = test.sheets()[0].cell(row, 5).value
        Desc = test.sheets()[0].cell(row, 2).value
        Server = test.sheets()[0].cell(row, 1).value
        f.write('VERSION = "0.1"')
        f.write('\n' + 'TIME = "20201015111814"')
        f.write('\n' + 'SYSTEM = "PR0"')
        f.write('\n' + 'RELEASE = "750"')
        f.write('\n' + '* DEVICE = {')
        f.write('\n' + '  NAME = "' + PrinterName + '"')
        f.write('\n' + '  PADEST = "' + ShortName + '"')
        f.write('\n' + '  PATYPE = "'+ Driver + '"')
        f.write('\n' + '  PAMODEL = "'+ Model + '"')
        f.write('\n' + '  PACLASS = ""')
        f.write('\n' + '  PAARCHIVER = ""')
        f.write('\n' + '  PALANGU = ""')
        f.write('\n' + '  PADISABLED = ""') #second file needs to have X here
        f.write('\n' + '  PANOQUERY = ""')
        f.write('\n' + '  PADUPCOPY = ""')
        f.write('\n' + '  PADUPLEX = ""')
        f.write('\n' + '  PASTORELOC = ""')
        f.write('\n' + '  PADFLTUTTL = ""')
        f.write('\n' + '  PADFLTSTTL = ""')
        f.write('\n' + '  PASYNC = ""')
        f.write('\n' + '  PAMONI = ""')
        f.write('\n' + '  PASTANDORT = "' + Desc + '"')
        f.write('\n' + '  PAMSG = "' + Desc + '"')
        f.write('\n' + '  PAMSSERVER = "SERVER_0"')
        f.write('\n' + '  PAMETHOD = "C"')
        f.write('\n' + '  PAPROSNAME = "\\' + Server + "\" + PrinterName + '"')
        f.write('\n' + '  PALOMS = ""')
        f.write('\n' + '  PALPRCMD = ""')
        f.write('\n' + '  PALPDHOST = ""')
        f.write('\n' + '  PALPDPORT = "0"')
        f.write('\n' + '  PACONNTIME = "0"')
        f.write('\n' + '  PAREADTIME = "0"')
        f.write('\n' + '  PATRAYS = "0"')
        f.write('\n' + '  PAXSHIFT = "0"')
        f.write('\n' + '  PAYSHIFT = "0"')
        f.write('\n' + '  PAXSHUNIT = "MM"')
        f.write('\n' + '  PAYSHUNIT = "MM"')
        f.write('\n' + '  PACRYPTMOD = ""')
        f.write('\n' + '  PACRYPTMET = ""')
        f.write('\n' + '  PANOVSERVR = ""')
        f.write('\n' + '  PAPOOLART = "P"')
        f.write('\n' + '  PATRACE2 = ""')
        f.write('\n' + '  PATRACEF = ""')
        f.write('\n' + '  PAPROTDATA = ""')
        f.write('\n' + '  PAPROTCMD = ""')
        f.write('\n' + '  PAPROTRES = ""')
        f.write('\n' + '  PAKEEPFILE = ""')
        f.write('\n' + '  CHGNAME1 = ""')
        f.write('\n' + '  CHGSAPREL1 = "750"')
        f.write('\n' + '  CHGSAPSYS1 = "PR0"')
        f.write('\n' + '  PADEVGRP = ""')
        f.write('\n' + '  COLORPRT = "X"')
        f.write('\n' + '  PRINTMODE = ""')
        f.write('\n' + '  INPUTTRAY = ""')
        f.write('\n' + '  PANOCHOICE = ""')
        f.write('\n' + '}')