0

I'm using code to copy a df to an excel spreadsheet starting in cell A2.

Here's the code snippet. I'm including all the imports used in the code.

import pandas as pd
import numpy as np
import os
import pathlib
from pathlib import Path 
import win32com.client as win32
import datetime as dt 
from datetime import timedelta
import openpyxl as xl
import xlwings as xw 

NewPortfolio = Path.home().joinpath("Desktop", "test.xlsx")
wb1 = Path.home().joinpath("Desktop", "test2.xlsx")

NewPortfolio_pd = pd.read_excel(NewPortfolio)

app = xw.App(visible=False)
wb = xw.Book(wb1)
ws = wb.sheets['Sheet1']

ws.range('A2').options(index=False).value = NewPortfolio_pd

wb.save()
wb.close()
app.quit()

Here's the error

AttributeError: module 'win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9' has no attribute 'CLSIDToPackageMap'

I've already tried uninstalling and reinstalling pywin32, and have not been able to find a solution online at the moment.

If you have any comments or recommendations it'd be greatly appreciated

Thanks in advance!

CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • https://support.pyxll.com/hc/en-gb/articles/360058200414-WARNING-Delete-your-gen-py-folder-C-Users-xxx-AppData-Local-Temp-gen-py-x-x-as-it-may-have-become-corrupted, – CristiFati Jan 25 '23 at 12:41
  • Definitely try what @CristiFati suggests. Also what line is causing the error? – DS_London Jan 25 '23 at 14:53
  • @DS_London this is caused by this line `app = xw.App(visible=False)` – Jose Daniel Rojas Jan 25 '23 at 15:47
  • Does this solve your problem? https://stackoverflow.com/q/23864234/4788546. – CristiFati Jan 25 '23 at 23:45
  • It did not, I went to the `%temp%` folder, `gen_py` and erased the `__pycache__` folder. That did it. Now I'm trying to get some code that will erase that file at the beginning of the code once. I tried to write it myself, but the code looped over the file, erasing it multiple times, which in turn crashed the code. – Jose Daniel Rojas Jan 26 '23 at 21:56

1 Answers1

-1

Just a guess

but swap

import win32com.client as win32

with

import win32api as win32
Necrosis
  • 35
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 25 '23 at 16:31
  • This is completely wrong. *win32* is not used in the code, so if suspecting that it could have something to do with the import side effects, then it can be removed altogether. – CristiFati Jan 27 '23 at 09:53