Python Forum
Python best library for Excel reports & review of existing code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python best library for Excel reports & review of existing code
#1
I need to create excel report (update data in existing file) based on variety source data (also excel files). All data will be .xlsx. In general steps that I'll do are:

1.Download settings information;
2.Create "starting" reports based on settings information;
3.Filter & Copy & paste data to created reports (in some of the cases data starts from as example B3:T10 - so not always A1);
4.Copy & paste (autofill) formulas.
5.Launch macro (if necessary I can try rewrite macro code in Python)
I've tried to use openpyxl and pandas but I find it as not stable solution. As example if I run code for 1st time it's working but in 2nd approach I receive errors like

-"BadZipFile: File is not a zip file" -> (if I upload starting excel that will be updated then issue disappears);
-"template = load_workbook("_MAT_TEMPLATE.xlsx") KeyError: "There is no item named '[Content_Types].xml' in the archive" -> again uploading file resolves the issue;
-ValueError: I/O operation on closed file.-> issue when I want to save excel after pasting pandas dataframe - not solved yet.

As I haven't work on such complex topic yet, could you advice what library I could use for working on such task? I've tired openpyxl and pandas but I feel that they are somehow unstable. I've learned about Spire.XLS but seems that this is paid solution. If you also know already existing project on github that I could use as "inspiration" I would be also grateful.

Best regards, Jakub

What I did so far is

from  openpyxl.workbook import workbook
from  openpyxl import load_workbook 
import pandas as pd
import xlsxwriter
xlPY = load_workbook("FX_rates_PY.xlsx")
wsPY = xlPY["englisch"]
FXPY = wsPY["B7:u32"]

template = load_workbook("_MAT_TEMPLATE.xlsx")
templateFXPY = template["FX rates_PY"]
materials = template["Materials"]
dest_rangePY = templateFXPY["A1:T26"]
templateFXCY = template["FX rates_CY"]
dest_rangeCY = templateFXCY["A1:T26"]

for i, row in enumerate(FXPY):
    for j, cell in enumerate(row):
        dest_rangePY[i][j].value=cell.value   
    
xlCY = load_workbook("FX_rates_CY.xlsx")
wsCY = xlCY["englisch"]
FXCY = wsCY["B7:U32"]

for i, row in enumerate(FXCY):
    for j, cell in enumerate(row):
        dest_rangeCY[i][j].value=cell.value   
    
    
cover_sheet = template["Cover"]
cover_sheet["C66"] = "Y"
cover_sheet["C55"] = "Y"


Month = cover_sheet['c7'].value
CurrentYear = cover_sheet['c5'].value

Bot_input = template["BOT Input"]
data_values = []
for row in Bot_input:
    data_values.append([cell.value for cell in row])

PlantDataClean = pd.DataFrame(data_values[1:], columns=data_values[0])
PlantDataClean = PlantDataClean[PlantDataClean["GRIP Plant"].notna()] 
PlantDataClean['GRIP Plant'] = PlantDataClean['GRIP Plant'].astype(int)


print(PlantDataClean)


erp_value = PlantDataClean['ERP'].values[0]
SAP_Plant_value = PlantDataClean['SAP Plant'].values[0]
print(SAP_Plant_value)

from openpyxl.utils.dataframe import dataframe_to_rows


if erp_value == "BKP":
    BKP = load_workbook("BKP_Material List.xlsx")
    BKPsheet = BKP["Sheet1"]

    data_values = []
    for row in BKPsheet:
        data_values.append([cell.value for cell in row])
    BKPsheetPD = pd.DataFrame(data_values[1:], columns=data_values[0])
    BKPsheetPDfilter = BKPsheetPD[BKPsheetPD["Plant"] ==                 
SAP_Plant_value]
    BKPsheetPDfilter =BKPsheetPDfilter["Material"]
    print(BKPsheetPDfilter)


    BKPsheetPDfilter.to_excel('_MAT_TEMPLATE.xlsx',sheet_name="sheet1")
    #,startcol=2,startrow=0,header=None)

else:
    print("S4 not avaiable yet")    
template.save("_MAT_TEMPLATE.xlsx")
Reply


Messages In This Thread
Python best library for Excel reports & review of existing code - by MasterOfDestr - Feb-13-2024, 06:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python beginner that needs an expression added to existing script markham 1 753 Sep-04-2023, 05:24 AM
Last Post: Pedroski55
  add mqtt feature to existing code Positron79 0 623 Jan-31-2023, 05:56 PM
Last Post: Positron79
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 2,118 Dec-17-2022, 12:24 AM
Last Post: snippsat
  Catching a crash within a library code ebolisa 9 3,312 Nov-22-2021, 11:02 AM
Last Post: bowlofred
  How do I open the Source code of a library? JaneTan 1 2,347 Aug-18-2021, 02:12 AM
Last Post: Larz60+
  Copy column from one existing excel file to another file mkujawsk 0 5,819 Apr-14-2021, 06:33 PM
Last Post: mkujawsk
  Help with Creating a Script for Automating Reports SunWers 1 1,977 Dec-29-2020, 10:21 PM
Last Post: jjc385
  How to add an image to an existing facebook post using python graph API? Ascalon 0 2,283 Dec-01-2020, 08:33 PM
Last Post: Ascalon
  Writing to existing excel sheet jksvend 0 1,988 Oct-12-2020, 11:19 AM
Last Post: jksvend
  Python Paramiko mkdir command overwriting existing folder. How can i stop that? therenaydin 1 3,302 Aug-02-2020, 11:13 PM
Last Post: therenaydin

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020