Re: Converting images to PDF. Final file has blank pages before and after.
On 2020-12-03 03:32, Michael Baca wrote: On Monday, November 30, 2020 at 7:15:37 PM UTC-7, MRAB wrote: On 2020-12-01 01:20, Michael Baca wrote: > Hello, new to the group, rather new to programming. > > I'm writing a program that takes images and converts them into PDF's. It works after quite a few days of trying, however the final file has a blank page inserted before and after each page containing the images. > > This uses FPDF to do the conversion. I've been up and down trying to figure out where I'm adding an extra page, so it might be an FPDF issue. > > def multi_convert(pdf_Filename, file_path): > if (dir): > file_list = [] > print(""), print("") > print("Converting... This may take awhile depending on the number of images.") > > for entry in os.scandir(file_path): > if (entry.path.endswith(".jpg") or entry.path.endswith(".png")) and entry.is_file(): > file_list.append(entry.path) > else: > print("Error: ") > print("Invalid Directory - {}", dir) > cover = Image.open(str(file_list[0])) > width, height = cover.size > > pdf = FPDF(unit="pt", format=[width, height]) > > for page in file_list: > pdf.add_page() > pdf.image(str(page)) > > pdf.output(file_path + pdf_Filename + ".pdf", "F") > exit() > It says in the documentation for the .image method: """ x: Abscissa of the upper-left corner. If not specified or equal to None, the current abscissa is used (version 1.7.1 and up). y: Ordinate of the upper-left corner. If not specified or equal to None, the current ordinate is used; moreover, a page break is triggered first if necessary (in case automatic page breaking is enabled) and, after the call, the current ordinate is moved to the bottom of the image (version 1.7.1 and up). """ In other words, you're not specifying where the top-left corner of the image should go, so it's putting it at the current position, wherever that is, and it's responding to the positioning by inserting additional pages. The solution is to specify the images' positions, and, perhaps, also their sizes, if necessary. By the way, why doesn't the function end with "exit()"? That'll make it exit the Python completely; rarely a good idea. Thank you very much for the help. That makes sense, as I remember reading about the image placement, but I didn't think it mattered. My use is very simple, I occasionally need to covert images into to PDF's and this makes it easier than going online. So I figured it was okay to leave that out. I need to go back and read the manual some more. It's a CLI program, it takes a few different arguments and when it's done it just returns to the shell. That's why I have the exit() where I do. Is there a better/safer way to exit the program? It's best if a function just returns to its caller. That's what you'd expect a function to do, and it's a good practice to follow because it reduces the number of unexpected "surprises" in the long term or if you come back to the code much later. -- https://mail.python.org/mailman/listinfo/python-list
Unable to read .xlsx file using pandas
Kindly help manage read .xlsx files using pandas, thank you import numpy as np import pandas as pd from pandas import Series, DataFrame excelfile = pd.ExcelFile('C:\Users\THOMAS\Documents/Hash Analytics Internship - DemoS2.xlsx') dframe = excelfile.parse('Sheet10') print (dframe) C:\Users\THOMAS\AppData\Local\Programs\Python\Python39\python.exe "C:/Users/THOMAS/PycharmProjects/Assignment#2/code File.py" File "C:\Users\THOMAS\PycharmProjects\Assignment#2\code File.py", line 5 excelfile = pd.ExcelFile('C:\Users\THOMAS\Documents/Hash Analytics Internship - DemoS2.xlsx') ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \U escape Process finished with exit code 1 -- Best Regards, *A. M. Thomas* Electrical & Solar Systems Expert *THE GUY BEHIND THE SCENE* Cell: +254 723 309 157 / +254 731 406 125 Email: thomaso...@gmail.com / thomaso.engin...@gmail.com "Alternative energy for real and sustainable development that we need" -- https://mail.python.org/mailman/listinfo/python-list
Re: Unable to read .xlsx file using pandas
On 2020-12-03 12:13, A. M. Thomas [PETech MIET MBA] wrote: Kindly help manage read .xlsx files using pandas, thank you import numpy as np import pandas as pd from pandas import Series, DataFrame excelfile = pd.ExcelFile('C:\Users\THOMAS\Documents/Hash Analytics Internship - DemoS2.xlsx') dframe = excelfile.parse('Sheet10') print (dframe) C:\Users\THOMAS\AppData\Local\Programs\Python\Python39\python.exe "C:/Users/THOMAS/PycharmProjects/Assignment#2/code File.py" File "C:\Users\THOMAS\PycharmProjects\Assignment#2\code File.py", line 5 excelfile = pd.ExcelFile('C:\Users\THOMAS\Documents/Hash Analytics Internship - DemoS2.xlsx') ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \U escape Process finished with exit code 1 You're giving the path as a plain string that contains \U, which is the start of an escape sequence, but it's not a valid escape sequence, hence the error. You could use a raw string: r'C:\Users\THOMAS\Documents/Hash Analytics Internship - DemoS2.xlsx' or escape the backslashes: 'C:\\Users\\THOMAS\\Documents/Hash Analytics Internship - DemoS2.xlsx' or use slashes instead of backslashes: 'C:/Users/THOMAS/Documents/Hash Analytics Internship - DemoS2.xlsx' -- https://mail.python.org/mailman/listinfo/python-list
Observing long running processes of Jupyter Notebook
We have been running Jupyter Notebook processes, which take long time to run. We use nbconvert to run these in commandline. Nbconvert only writes output into a file at the end. We just wonder whether there is a way to observe the progress and printing messages when nbconvert is running. Regards, David -- https://mail.python.org/mailman/listinfo/python-list
Re: Observing long running processes of Jupyter Notebook
On Thu, 3 Dec 2020 at 14:12, Shaozhong SHI wrote: > > We have been running Jupyter Notebook processes, which take long time to > run. > > We use nbconvert to run these in commandline. Nbconvert only writes output > into a file at the end. > > We just wonder whether there is a way to observe the progress and printing > messages when nbconvert is running. If you're under linux, you can use `tail -f FILE` in the command line. -- https://mail.python.org/mailman/listinfo/python-list
help(list[int]) → TypeError
Is this the correct behavior? Python 3.9.0 (default, Oct 7 2020, 23:09:01) [GCC 10.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> help(list[int]) Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.9/_sitebuiltins.py", line 103, in __call__ return pydoc.help(*args, **kwds) File "/usr/lib/python3.9/pydoc.py", line 2001, in __call__ self.help(request) File "/usr/lib/python3.9/pydoc.py", line 2060, in help else: doc(request, 'Help on %s:', output=self._output) File "/usr/lib/python3.9/pydoc.py", line 1779, in doc pager(render_doc(thing, title, forceload)) File "/usr/lib/python3.9/pydoc.py", line 1772, in render_doc return title % desc + '\n\n' + renderer.document(object, name) File "/usr/lib/python3.9/pydoc.py", line 473, in document if inspect.isclass(object): return self.docclass(*args) File "/usr/lib/python3.9/pydoc.py", line 1343, in docclass (str(cls.__name__) for cls in type.__subclasses__(object) TypeError: descriptor '__subclasses__' for 'type' objects doesn't apply to a 'types.GenericAlias' object >>> I would have expected the output to the identical to help(list). -- https://mail.python.org/mailman/listinfo/python-list
list[type, type, ...] ?!
Using the typing.List generic alias, I can only specify a single type. Example: >>> typing.List[int] typing.List[int] When I try to specify additional types, it fails. Example: >>> typing.List[int, int] Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.9/typing.py", line 243, in inner return func(*args, **kwds) File "/usr/lib/python3.9/typing.py", line 775, in __getitem__ _check_generic(self, params, self._nparams) File "/usr/lib/python3.9/typing.py", line 197, in _check_generic raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};" TypeError: Too many parameters for typing.List; actual 2, expected 1 This makes sense to me. An item has one type, and we use Union if we want variants. What's not making sense to me in Python 3.9: I can use the built-in generic alias in list in this manner, apparently successfully: >>> list[int, int] list[int, int] In fact, it appears I can specify an indeterminate number of types. Can someone explain what this construct means? I suspect this will fail to be interpreted by type validators, but wonder why it doesn't fail fast when I express it. Thanks, Paul -- https://mail.python.org/mailman/listinfo/python-list
Re: list[type, type, ...] ?!
On 3/12/20 7:37 pm, Paul Bryan wrote: list[int, int] list[int, int] In fact, it appears I can specify an indeterminate number of types. I think the built-in generic alias just provides the minimum necessary to be able to write sometype[arg, ...]. It doesn't know anything about the semantics with respect to particular types -- that's left to the type checkers. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: list[type, type, ...] ?!
On 4/12/20 12:31 pm, Paul Bryan wrote: Would it make sense for list's __class_getitem__ (GenericAlias?) to perform similar checking as typing._SpecialGenericAlias (nparams)? Maybe. It's a slippery slope -- how much of the typing module do we want to drag into the core of the interpreter? -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: list[type, type, ...] ?!
Thanks, Greg. Would it make sense for list's __class_getitem__ (GenericAlias?) to perform similar checking as typing._SpecialGenericAlias (nparams)? On Fri, 2020-12-04 at 12:15 +1300, Greg Ewing wrote: > On 3/12/20 7:37 pm, Paul Bryan wrote: > > > > > list[int, int] > > list[int, int] > > > > In fact, it appears I can specify an indeterminate number of types. > > I think the built-in generic alias just provides the minimum > necessary to be able to write sometype[arg, ...]. It doesn't > know anything about the semantics with respect to particular > types -- that's left to the type checkers. > > -- > Greg -- https://mail.python.org/mailman/listinfo/python-list