dropdown menu

Back To Home

Monday, June 29, 2020

Python - Read open excel file data

Sometimes we want to read real time update data for excel that haven't save.
So below code is useful.


Python code:

import win32com.client as win32
import pandas as pd

#here we use win32com to save open excel
excel = win32.gencache.EnsureDispatch('Excel.Application')
print("Active WB:", excel.ActiveWorkbook.Name)
for wb in excel.Workbooks:
    if wb.Name == 'da.xlsx' :

        print("WB:",wb.Name)
        wb.Save()

#here we use panda module to read and check
df = pd.read_excel (r'da.xlsx', sheet_name='Sheet1')
print (df)


Monday, May 25, 2020

Solved: UnicodeEncodeError: 'charmap' codec can't encode characters

Below are code to solve this error.

Code:


import xlsxwriter 
  
workbook = xlsxwriter.Workbook('skill.csv') 
  
# By default worksheet names in the spreadsheet will be  
# Sheet1, Sheet2 etc., but we can also specify a name. 
worksheet = workbook.add_worksheet("skill") 
  
# Some data we want to write to the worksheet. 
details = ( 
    ['SN', 'Name','Contribution'], 
    [1, "Linus Torvalds", "Linux Kernel"], 
    [2, "Tim Berners-Lee", "World Wide Web"], 
    [3, "Guido van Rossum", "Python Programming"],
    [4, "吴刚", "中文"],
 ) 

# Start from the first cell. Rows and 
# columns are zero indexed. 
row = 0
col = 0
  
# Iterate over the data and write it out row by row. 
for SN, Name , Contribution in (details): 
    worksheet.write(row, col, SN) 
    worksheet.write(row, col + 1, Name)
    worksheet.write(row, col + 2, Contribution)
    row += 1
  
workbook.close() 




Saturday, May 23, 2020

python - table generation in pdf using python

First need to install module reportlab. Then create table using code below:

Output pdf:



Code:

from reportlab.lib import colors   #import module
from reportlab.lib.pagesizes import letter, inch
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle

doc = SimpleDocTemplate("simple_table_grid.pdf", pagesize=letter)   #define pdf document name
# container for the 'Flowable' objects
elements = []   #define the data

data= [['00', '01', '02', '03', '04'],
       ['10', '11', '12', '13', '14'],
       ['20', '21', '22', '23', '24'],
       ['30', '31', '32', '33', '34']]
t=Table(data,5*[0.8*inch], 4*[0.8*inch])     #define the table format width and legth
t.setStyle(TableStyle([('ALIGN',(1,1),(-2,-2),'RIGHT'),
                       ('TEXTCOLOR',(1,1),(-2,-2),colors.red),
                       ('VALIGN',(0,0),(0,-1),'TOP'),
                       ('TEXTCOLOR',(0,0),(0,-1),colors.blue),
                       ('ALIGN',(0,-1),(-1,-1),'CENTER'),
                       ('VALIGN',(0,-1),(-1,-1),'MIDDLE'),
                       ('TEXTCOLOR',(0,-1),(-1,-1),colors.green),
                       ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                       ('BOX', (0,0), (-1,-1), 0.25, colors.black),
                       ]))

elements.append(t)        #then write the data
# write the document to disk

doc.build(elements)




Tuesday, May 5, 2020

Python tkinter - textbox


Here is the code for python gui textbox.


Python code:

from tkinter import *

root=Tk()

name=StringVar()

a=Entry(root,textvariable=name)
a.pack()

def enter():
    print(name.get())

    print("the text we get"+name.get())


b=Button(root,text="enter",command=enter)
b.pack()

root.mainloop()

Saturday, April 11, 2020

No module named PIL solved

No module named PIL solved.
At command propmpt: pip install pillow
 

Install free python IDE for window

Here is the step to install free python IDE for window.


No module named cv2 solved

No module named cv2 solved.

At command prompt type: pip install opencv-python.