1. 程式人生 > 其它 >往Excel填資料

往Excel填資料

import pandas as pd
import os
import openpyxl
from openpyxl import load_workbook
# import xlrd
# import xlwt
# from xlutils.copy import copy

jsonObj = ${input}
typeOfJsonObj = type(jsonObj)
print("typeOfJsonObj:",typeOfJsonObj)
print("jsonObj:",jsonObj)

customerPoNo = jsonObj["customerPoNo"]
print
("customerPoNo:",customerPoNo) shipmentNo = jsonObj["shipmentNo"] print("shipmentNo:",shipmentNo) sourceLocation = jsonObj["sourceLocation"] print("sourceLocation:",sourceLocation) estimatedArrivalDate = jsonObj["estimatedArrivalDate"] print("estimatedArrivalDate:",estimatedArrivalDate) totalItemPackageCount
= jsonObj["totalItemPackageCount"] print("totalItemPackageCount:",totalItemPackageCount) submitTenderDate = jsonObj["submitTenderDate"] print("submitTenderDate:",submitTenderDate) totalGrossWeight = jsonObj["totalGrossWeight"] print("totalGrossWeight:",totalGrossWeight) detailDeliveryAddress = jsonObj["
detailDeliveryAddress"] print("detailDeliveryAddress:",detailDeliveryAddress) lineInformation = jsonObj["lineInformation"] typeOfJsonlineInformation = type(lineInformation) print("typeOfJsonlineInformation:",typeOfJsonlineInformation) print("lineInformation:",lineInformation) file = POtemplate print("file:",file) filePath = file.replace("\\", "\\\\") file = eval(repr(filePath).replace('\\\\', '\\')) print("file:",file) wb = openpyxl.load_workbook(file) # 獲取workbook中所有的表格 sheets = wb.sheetnames sheetsOfType = type(sheets) print("sheetsOfType:",sheetsOfType) sheet = wb['DN_1'] sheet.cell(2, 2).value = customerPoNo sheet.cell(6, 2).value = shipmentNo sheet.cell(7, 2).value = detailDeliveryAddress sheet.cell(9, 2).value = totalItemPackageCount sheet.cell(10, 2).value = totalGrossWeight sheet.cell(11, 2).value = submitTenderDate sheet.cell(12, 2).value = estimatedArrivalDate sheet.cell(13, 2).value = sourceLocation index = 0 for ele in lineInformation: typeOfEle = type(ele) print(typeOfEle) quantity = ele["quantity"] print("quantity:",quantity) sheet.cell(18+index, 4).value = quantity itemCode = ele["itemCode"] print("itemCode:",itemCode) sheet.cell(18+index, 3).value = itemCode engDescription = ele["engDescription"] print("engDescription:",engDescription) sheet.cell(18+index, 2).value = engDescription barcode = ele["barcode"] print("barcode:",barcode) sheet.cell(18+index, 5).value = barcode index = index + 1 wb.save(file)