by KoderShop | May 24, 2023
To resolve this issue, you can try installing the Tkinter module using the appropriate package manager based on your operating system. For example:
- For Ubuntu or other Linux distributions using Apt, you can use the following command:
sudo apt-get install python3-tk
- If you’re using Fedora, the command would be:
sudo dnf install python3-tkinter
- Alternatively, you can specify a specific Python version during installation. For instance:
sudo apt-get install python3.7-tk
sudo dnf install python3-tkinter-3.6.6-1.fc28.x86_64
Once the installation is complete, you can import the module using the appropriate name for your Python version. For Python 3, use import tkinter as tk, and for Python 2, use import Tkinter as tk. If you want compatibility with both Python versions, you can choose at runtime based on the version number of the Python interpreter:
import sys
if sys.version_info[0] == 3:
import tkinter as tk
else:
import Tkinter as tk
By following these steps, you should be able to resolve the ModuleNotFoundError and successfully import the Tkinter module in Python.
by KoderShop | May 5, 2023
Styles in Excel are a powerful tool to enhance the visual appeal of your data by changing the way it looks on screen. They can also be used to determine the formatting for numbers, making your data more readable and easier to understand.
With openpyxl, you can apply styles to various aspects of your Excel file, such as the font, fill, border, cell alignment, and protection. By setting these properties, you can control the visual appearance of your data and make it more visually appealing and easier to read.
For example, you can use openpyxl to set the font size, color, and underlining of your data, as well as to apply patterns or color gradients to the background of your cells. You can also set borders on your cells to make them stand out, and adjust cell alignment to control the position of your data within the cell.
Another useful feature of openpyxl is the ability to apply conditional formatting, which allows you to apply styles to your data based on certain conditions. For example, you can use conditional formatting to apply font colors based on the text content of the cell while preserving the original formatting of the cell, such as font family, font size, and font style.
By default, openpyxl provides a set of predefined styles that you can use to format your data. These styles provide default values for various formatting properties, such as font size, font family, and font style. You can also create your own custom styles to apply specific formatting to your data, such as a unique font size or color scheme.
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
font = Font(name='Calibri',size=11,bold=False,italic=False,vertAlign=None,underline='none',strike=False,color='FF000000')
fill = PatternFill(fill_type=None,start_color='FFFFFFFF',end_color='FF000000')
border = Border(left=Side(border_style=None,color='FF000000'),right=Side(border_style=None,color='FF000000'),top=Side(border_style=None,color='FF000000'),bottom=Side(border_style=None,color='FF000000'),diagonal=Side(border_style=None,color='FF000000'),diagonal_direction=0,outline=Side(border_style=None,color='FF000000'),vertical=Side(border_style=None,color='FF000000'),horizontal=Side(border_style=None,color='FF000000'))
alignment=Alignment(horizontal='general',vertical='bottom',text_rotation=0,wrap_text=False,shrink_to_fit=False,indent=0)
number_format = 'General'
protection = Protection(locked=True,hidden=False)
Cell Styles
Cell styles are shared between objects and once they have been assigned they cannot be changed. This stops unwanted side-effects such as changing the style for lots of cells when only one changes.
from openpyxl.styles import colors
from openpyxl.styles import Font, Color
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
B1 = ws['B1']
E4 = ws['E4']
ft = Font(color="FF0000")
B1.font = ft
E4.font = ft
B1.font.italic = True
B1.font = Font(color="FF0000", italic=True)
Copying styles
Styles can also be copied:
from openpyxl.styles import Font
from copy import copy
font1 = Font(name='Arial', size=14)
font2 = copy(ft1)
font2.name = "Tahoma"
font1.name
font2.name
font2.size
#Output:
#'Arial'
#'Tahoma'
#14
Indexed colours
Index
![Multiple font colors openpyxl](https://kodershop.com/wp-content/uploads/2023/05/Multiple-font-colors-openpyxl-1.jpg)
Applying Styles
Styles are applied directly to cells
from openpyxl.workbook import Workbook
from openpyxl.styles import Font, Fill
workbook = Workbook()
workbooks = workbook.active
c = workbooks['A1']
c.font = Font(size=12)
Styles can also be applied to columns and rows but note that this applies only to cells created (in Excel) after the file is closed. If you want to apply styles to entire rows and columns then you must apply the style to each cell yourself. This is a restriction of the file format:
column = workbooks.column_dimensions['A']
column.font = Font(bold=True)
row = workbooks.row_dimensions[1]
row.font = Font(underline="single")
Styling Merged Cells
The behavior of a merged cell is quite similar to that of other cell objects. Its value and format are primarily defined in the top-left cell. To modify the border of the entire merged cell, you will need to adjust the border of its top-left cell. It’s essential to note that the formatting is generated for the sole purpose of writing.
from openpyxl.styles import Border, Side, PatternFill, Font, GradientFill, Alignment
from openpyxl import Workbook
workbook = Workbook()
workbooks = workbook.active
workbooks.merge_cells('B2:F4')
top_left_cell = workbooks['B2']
top_left_cell.value = "My Cell"
thin = Side(border_style="thin", color="000000")
double = Side(border_style="double", color="ff0000")
top_left_cell.border = Border(top=double, left=thin, right=thin, bottom=double)
top_left_cell.fill = PatternFill("solid", fgColor="DDDDDD")
top_left_cell.fill = fill = GradientFill(stop=("000000", "FFFFFF"))
top_left_cell.font = Font(b=True, color="FF0000")
top_left_cell.alignment = Alignment(horizontal="center", vertical="center")
workbook.save("styled.xlsx")
by KoderShop | May 3, 2023
Social media is a powerful tool for businesses, marketers, and researchers to gather insights and analyze trends. Snscrape is a Python library that enables you to retrieve tweets containing a specific hashtag or keyword. This library is easy to use and customizable, making it a valuable resource for anyone seeking to analyze social media data.
Snscrape is a powerful and flexible Python library for scraping social media data. It’s designed to work with a variety of social media platforms, including Twitter, Reddit, Instagram, and others. With snscrape, you can easily retrieve social media data like tweets, posts, comments, and more, using simple and customizable Python code.
One of the great things about snscrape is its flexibility. You can customize your search by using a wide range of parameters, including keywords, hashtags, dates, and more. This allows you to retrieve only the data that’s relevant to your specific needs. Additionally, snscrape makes it easy to output your data in a variety of formats, including JSON, CSV, and others.
Installation
To install snscrape in Python, you can use the pip package manager. First, open up a terminal or command prompt and run the following command:
pip install snscrape
This will download and install the latest version of snscrape and its dependencies. Once the installation is complete, you can import snscrape in your Python script and start using it to scrape social media data.
If you encounter any issues with the installation, make sure that you have the latest version of pip installed on your system. You can upgrade pip by running the following command:
pip install --upgrade pip
Importing snscrape
Once you have installed the library, you can begin by importing snscrape in your Python code.
To retrieve a specified number of tweets containing a particular hashtag:
import snscrape.modules.twitter as sntwitter
# Define the search query
search_query = '#datascience since:2020-01-01 until:2020-12-31'
# Define the number of tweets to retrieve
num_tweets = 1000
# Create an empty list to store the tweets
tweets = []
# Iterate through the search results and append each tweet to the list
for i, tweet in enumerate(sntwitter.TwitterSearchScraper(search_query).get_items()):
if i >= num_tweets:
break
tweets.append(tweet)
In this example, we are searching for tweets containing the keyword “your_keyword” and limiting the results to 10 tweets. The code uses a for loop to iterate over the search results and print the content of each tweet. Note that you can customize the time range of the search by changing the “since” and “until” parameters.
Snscrape possibilities
Snscrape offers several parameters that allow you to customize your search. For example, you can filter by language, location, and user. You can also sort the results by date or popularity. The syntax for these parameters is straightforward and well-documented in the snscrape documentation.
To customize the search parameters, you can modify the search query string to include additional parameters such as language, location, and user:
# Example search query with additional parameters
search_query = 'data science lang:en near:"New York City" from:JohnDoe since:2020-01-01 until:2020-12-31'
The output format of snscrape is a JSON object containing information about each tweet, including the tweet’s text, user information, timestamp, and metadata. This format is flexible and easy to parse, making it ideal for data analysis and visualization.
The output of snscrape is a list of Tweet objects, which contain a variety of information about each tweet such as the text, author, date, and location. Here is an example of how to access and print the text of each tweet in the list:
# Print the text of each tweet in the list
for tweet in tweets:
print(tweet.content)
One of the benefits of using snscrape is that it allows you to retrieve tweets that are not available through Twitter’s API. This is because snscrape uses web scraping to retrieve data directly from Twitter’s website. This means that you can retrieve tweets that are not accessible through the API, such as deleted tweets or tweets that have been removed from public view.
You can also save the list of tweets to a file in a variety of formats such as JSON or CSV. Here is an example of how to save the tweets to a CSV file:
import csv
# Define the output file name
output_file = 'tweets.csv'
# Open the output file and write the tweets to it
with open(output_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['text', 'author', 'date', 'location'])
for tweet in tweets:
writer.writerow([tweet.content, tweet.user.username, tweet.date, tweet.location])
However, it is important to note that snscrape’s use of web scraping may violate Twitter’s terms of service. Therefore, it is important to use snscrape responsibly and to be aware of any legal or ethical implications of using web scraping to gather data.
Retrieve data from other medias
In addition to Twitter, snscrape can be used to scrape data from platforms such as Reddit and Instagram, making it a versatile tool for social media data scraping. The process for scraping data from each platform may vary slightly, but snscrape provides a unified interface for accessing each platform’s data. For example, to scrape data from Reddit using snscrape, the user can specify the subreddit to scrape and any search keywords using the same syntax as for Twitter hashtags and keywords.
Retrieving Reddit posts from a particular subreddit:
import snscrape.modules.reddit as snreddit
# Define the subreddit to search for
subreddit = "learnpython"
# Define the maximum number of posts to retrieve
max_posts = 100
# Create a query string to search for the subreddit
query = f"subreddit:{subreddit}"
# Retrieve the posts
posts = []
for i, post in enumerate(snreddit.RedditScraper(query).get_items()):
if i >= max_posts:
break
posts.append(post)
Retrieving Instagram posts containing a particular hashtag:
import snscrape.modules.instagram as sninstagram
# Define the hashtag to search for
hashtag = "instatravel"
# Define the maximum number of posts to retrieve
max_posts = 100
# Create a query string to search for the hashtag
query = f"{hashtag}"
# Retrieve the posts
posts = []
for i, post in enumerate(sninstagram.InstagramHashtagScraper(query).get_items()):
if i >= max_posts:
break
posts.append(post)
In summary, snscrape is a powerful Python library that enables you to retrieve tweets containing a specific hashtag or keyword. With its customizable parameters and flexible output format, snscrape is a valuable resource for anyone seeking to analyze social media data. However, it is important to use snscrape responsibly and to be aware of any legal or ethical implications of using web scraping to gather data.
by | Oct 19, 2022
Continuous integration is a development automation method that is always referred to as “CI” in CI/CD. A successful CI involves building, testing, and merging new code changes to an app into a shared repository on a regular basis. It addresses the issue of having too many potentially incompatible branches of an app in development at the same time.
More here https://kodershop.com/blog/review_of_the_best_cd_ci_tools/
by | Aug 23, 2022
C#, similar to C++ and Java is a strongly typed language. In a strongly typed language, we must provide a variable type to indicate what type of data we are going to store. For Ex: integer, money, text, float etc.
Ex:
int myCounter = 0;
string firstname = “First”;
Recent Comments