I'm just running the following code, straight from this documentation/tutorial.
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Add()
wb.SaveAs('add_a_workbook.xlsx')
excel.Application.Quit()
And got this:
execfile(filename, namespace)
File "C:/Users/Username/Desktop/script.py", line 106, in <module>
wb = excel.Workbooks.Add()
File "C:\Users\Username\AppData\Local\Temp\gen_py\2.7\00020813-0000-0000-C000-000000000046x0x1x7\Workbooks.py", line 34, in Add
ret = self._oleobj_.InvokeTypes(181, LCID, 1, (13, 0), ((12, 17),),Template
TypeError: an integer is required
Does anyone have any idea why? I've tried using an xlsx vs. xls file, and changing the file address, and trying multiple examples from that tutorial, and they all give me similar errors, and I have no idea why.
I can get as far as wb = excel.Workbooks.Add() before I get the TypeError: an integer is required warning, and if I try wb = excel.Workbooks.Add, it will run and I won't get the error, but I can't do anything from there on.
Does anyone know what this is? Thanks in advance.
[Edit:]
I tried a word file for comparison and I works fine.
Does anyone know why one of these works and one doesn't?
word = win32.Dispatch('Word.Application')
word.Documents.Open('C:\Users\username\Desktop\test.docx')
excel = win32.Dispatch('Excel.Application')
excel.Workbooks.Open('C:\Users\username\Desktop\output.xlsx')
[Edit 2:]
Okay, I found the problem is with the Spyder IDE. If I write the same code in Anaconda, it'll work fine. Does anyone know why Anaconda works but Sypder doesn't? I checked the system paths and they're identical, and even trying to execute a .py program in Anaconda doesn't work.
I seem to be the only person on the internet with this problem, but my workaround was I used a different spyder python interpretter.
Python interpreter gave me all sorts of errors doing pretty much every win32com excel command, but IPython console works fine. No idea why.
Related
I am accessing an excel file on a server in the network with the code
import openpyxl
path = r"S:\dic1\dic2\dic3\file.xlsx"
wb = openpyxl.load_workbook(path)
This is working perfectly fine on my pc but on another pc with exactly the same version of python installed it gives me the following error:
raise TypeError("Value must be a sequence")
TypeError: Value must be a sequence
How is this even possible? Any help would be greatly appreciated!
Please Check openpyxl version.
I have a Python script (gui.py - a calculation program, written by somebody else). I try to run this from excel with input data from the same excel file. It works fine when I start the script as:
objShell.Run PythonExe & PythonScript3
In the python script I used the following to get f.ex data from H5 cell, works also fine:
import openpyxl
wb = openpyxl.load_workbook("01.xlsm")
ws = wb.active
mastNumber = ws['H5']
But I don't want to edit a lot in gui.py (have just simple changes) so I planned to use a 2nd script (caller.py) which gets the data from Excel, then I import this to gui.py and there I just use the variable from caller.py. It works also as long as I start gui.py directly. When I start it from Excel I get an error msg.
error msg
So as long as the flow is not Excel -> gui-py -> which imports caller to get data from same Excel file -everything works fine.
I am open to any solution for this problem or a completely new approach if there is better for somebody with limited programming skills.
Looking at the error, can you try putting complete path of the excel file at line wb = openpyxl.load_workbook("01.xlsm") instead of just 01.xlsm.
While generating pdf from excel sheet i am getting below error:
ws.ExportAsFixedFormat(0, save_as)
File "<COMObject <unknown>>", line 5, in ExportAsFixedFormat
com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024809), None)
Below is my code:
pythoncom.CoInitialize()
xlApp = client.Dispatch("Excel.Application")
logging.debug("Saving excel file {} to file {}".format(filename, save_as))
books = xlApp.Workbooks.Open(filename)
ws = books.Worksheets[0]
ws.Visible = 1
ws.ExportAsFixedFormat(0, save_as)
books.Close(True)
xlApp.Quit()
It works on my laptop where office 365 is installed however gives above error on another system with Microsoft office 2007 is installed.
Python version : python 2.7
openpyxl : 2.4.5
pywin32: 224
There is not enough documentation. It would be great help if someone can provide pointer to debug it and understand the error.
I had the same error while using xlwings for interacting with Excel. xlwings also use win32com clients in the backend.
I think it is not an Excel version issue.
I realized that this error pops up whenever the code is executed and the excel file (containing data) is not in focus. In order to resolve the issue, I simply select the file which is being processed and run the code and it always works for me.
I have found this related article: Python Interactions with Excel Macros
The code there is for python 2. The most important part is that closing the workbook and quitting the app resulted in remained excel workbooks hiddenly open in the background therefore when you want to open the workbook with Excel it can be open for reading only. To completely close all threads do this at the end (Python 3):
books.Close(True)
xlApp.quit()
xlApp = None
del xlApp
This way no threads remains and workbook(s) may be open for writing, can be saved...
This may also help in general error code decryption, in Python prompt:
import win32api
win32api.FormatMessage([ERRORCODEHERE])
for example:
import win32api
win32api.FormatMessage(-2147352567)
Which may give you more hints on the error (for me it did not).
I found the error to be raised when
wrong sheet name is given, so the excel file does not contain such
worksheet
while the script is running and performing tasks on the given
worksheet it loses somehow the focus. It may happen if you open or
just click on an excel file meanwhile the script is running, or you
open several workbooks with the script and it gets lost somehow.
Excel and python api should not have issues to work paralelly, but for me it did somehow.
Make everything step by step: open file->process->close then next...
All in short, check your code and worksheet names first, then go for simplified steps.
Why do I receive this warning message every time I run my code? (below). Is it possible to get rid of it? If so, how do I do that?
My code:
from openpyxl import load_workbook
from openpyxl import Workbook
wb = load_workbook('NFL.xlsx', data_only = True)
ws = wb.active
sh = wb["Sheet1"]
ptsDiff = (sh['J127'].value)
print ptsDiff
The code works but I get this warning message:
Warning (from warnings module):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/openpyxl/reader/worksheet.py", line 320
warn(msg)
UserWarning: Unknown extension is not supported and will be removed
This error happens when openpyxl cannot understand/read an extension (source). Here is the list of built-in extensions openpyxl currently knows that is doesn't support:
Conditional Formatting
Data Validation
Sparkline Group
Slicer List
Protected Range
Ignored Error
Web Extension
Slicer List
Timeline Ref
Also see the Worksheet extension list specification.
Try to add single quotes to your data_only parameter like this:
wb = load_workbook('NFL.xlsx', data_only = **'True'**)
This works for me.
Using python 3.5 under Anaconda3, Excel 2016, Windows10 -- I had the same problem initially with an xlsx file. Tried to make it into a csv and did not work. What worked was: select the entire spreadsheet, copy on a Notepad, select the notepad text, paste in a new spreadsheet, save as xslx. It looks like any extra formatting would result in a warning.
It is already listed in the first answer what is wrong with it If you only want to get rid of the error that is given in red for some reason. You can go to the file location of the error and # the line where is says warn(msg) this will stop the error being displayed the code still works fine in my experience.I am not sure if this will work after compiled but this should work in the same machine.
PS:I had the same error and this is what I did because I though it could be confusing for the end user
PS:You can use a try and except error catcher too but this is quicker.
I'm trying to read a file using python and I keep getting this error
ERROR: Line magic function `%user_vars` not found.
My code is very basic just
names = read_csv('Combined data.csv')
names.head()
I get this for anytime I try to read or open a file. I tried using this thread for help.
ERROR: Line magic function `%matplotlib` not found
I'm using enthought canopy and I have IPython version 2.4.1. I made sure to update using the IPython installation page for help. I'm not sure what's wrong because it should be very simple to open/read files. I even get this error for opening text files.
EDIT:
I imported traceback and used
print(traceback.format_exc())
But all I get is none printed. I'm not sure what that means.
Looks like you are using Pandas. Try the following (assuming your csv file is in the same path as the your script lib) and insert it one line at a time if you are using the IPython Shell:
import pandas as pd
names = pd.read_csv('Combined data.csv')
names.head()