I'm trying to create a DB from an excel spreadsheet. I can fetch data from excel and display in the html page, but I am not able to store it in sqlite db.
Few ways you can try:
Save excel as csv. Read csv in python (link) and save in sqlite (link).
Read excel into a pandas dataframe (link), and then save dataframe to sqlite (link).
Read excel directly from python (link) and save data to sqlite.
I used below code which worked but it over rights file.
#import pandas software library
import pandas as pd
df = pd.read_excel(r'C:\Users\kmc487\PycharmProjects\myproject\Product List.xlsx')
#Print sheet1
print(df)
df.to_excel("output.xlsx", sheet_name="Sheet_1")
Below are the input file details:
My input file is in .xlsx format and file is stored as .xls(Need code to .xlsx format)
File has heading in second row(First row blank)
Related
I want to save groupby dataframe in csv but i am trying to save in csv it is not saving groupby dataframe.
this is data :
dataframe image
i run this code df.groupby(['Date','Name']).sum() after that i got
output image of groupby dataframe
but i am trying to save in csv file it save like this
I run this code df.to_csv("abcd.csv")
csv file image
But I want to save in csv file like
saving excel file output which i want
please tell me the solution
thank you
CSV files are plain text, The agreed format is each row is separated by newline and each character is separated by , in general.
To achieve the formatting you want, you can convert the df into an excel file instead of csv
gdf = df.groupby(['Date','Name']).sum()
gdf.to_excel("<path_to_file>")
You will need to explicitly install xlwt to achieve working with excel files
pip install xlwt
My Python program converts Excel files (.xlsx) into a CSV file using Panda's read_excel and to_csv function, and at some point in the future, the CSV is converted back into an Excel file. Maintaining the data is fine, but of course all of the formatting and styling is gone. So I could use some help in being able to capture the that information to use when after converting the CSV back into an Excel file.
import pandas as pd
import xlsxwriter
EXCEL_PATH_FROM = r'C:\absolute\path\to\excel.xlsx'
EXCEL_PATH_TO = r'C:\absolute\path\to\other\excel.xlsx'
CSV_PATH = r'C:\absolute\path\to\csv.csv'
# read excel and convert to csv
def saveData():
read_excel = pd.read_excel(EXCEL_PATH_FROM)
print("writing csv...")
read_excel.to_csv(CSV_PATH, index=None, header=True)
# get csv data and import that data into an excel file
def createFromData():
csv = pd.read_csv(CSV_PATH)
excel = pd.ExcelWriter(EXCEL_PATH_TO, engine='xlsxwriter')
csv.to_excel(excel, index=None)
excel.save()
Some ideas I had were to save the Excel as a XML and insert format and style information as attributes or something, or to create both a CSV and XML from the Excel (one for data and one for styling). One problem I have is figuring out how to access that information.
Are there currently any packages that support Python 3 (currently using 3.8) that could help simplify this process? I dug through openpyxl's documentation and they have some stylesheet classes that aren't meant to be used directly I don't think and I couldn't figure out how to use them directly.
I am trying to add a large dataset to an existing xls spreadsheet.
I'm currently writing to it using a pandas dataframe and the .to_excel() function, however this erases the existing data in the (multi-sheet) workbook. The existing spreadsheet is very large and complex,it also interacts with several other files, so I can't convert it to xlsx or read and rewrite all of the data, as I've seen some suggestions on other questions. I want the data that I am adding to be pasted starting from a set row in an existing sheet.
Yes , you can use the library xlsxwriter , link= https://xlsxwriter.readthedocs.io
code example :
import xlsxwriter
Name="MyFile"+".xlsx"
workbook = xlsxwriter.Workbook(Name)
worksheet = workbook.add_worksheet()
worksheet.write("A1", "Incident category".decode("utf-8"))
worksheet.write("B1", "Longitude".decode("utf-8"))
worksheet.write("C1", "Latitude".decode("utf-8"))
workbook.close()
I have a public google sheet with 2 sheets https://docs.google.com/spreadsheets/d/14hFn00O9632n96Z2xGWvfrcY-K4kHiOGR02Rx7dsj54/edit#gid=447738801
I know how to wget this as a csv (if it had only 1 sheet), but is there a simple way of getting sheet 1 and sheet 2 as a dictionary or as a csv file (each sheet as 1 csv file) and I will parse it.
gid of both sheets are different
In the end I will have header as key and values below the header as values
Use the requests library to download each sheet, and write the response content to a file.
Working implementation:
import requests
sheets = {
'sheet1': 'https://docs.google.com/spreadsheets/d/14hFn00O9632n96Z2xGWvfrcY-K4kHiOGR02Rx7dsj54/export?format=csv&id=14hFn00O9632n96Z2xGWvfrcY-K4kHiOGR02Rx7dsj54&gid=0',
'sheet2': 'https://docs.google.com/spreadsheets/d/14hFn00O9632n96Z2xGWvfrcY-K4kHiOGR02Rx7dsj54/export?format=csv&id=14hFn00O9632n96Z2xGWvfrcY-K4kHiOGR02Rx7dsj54&gid=447738801'
}
for sheet in list(sheets.keys()):
response = requests.get(sheets[sheet])
with open(f'{sheet}.csv', 'wb') as csvfile:
csvfile.write(response.content)
This will save each sheet in a file (sheet1.csv and sheet2.csv in this case). Note that I got the link for each sheet just by downloading it as CSV from a browser and copying the download link.
You can then convert it to a dictionary using the CSV library. See this post.
Just use pandas to load CSV. Then you can convert into Dict or anything else later
import pandas as pd
# Read data from file 'filename.csv'
data = pd.read_csv("filename.csv")
data.to_dict('series')
I want to convert some data in my database to XLS (Excel) format. I used tablib to do this and can get the Excel sheets in the proper format.
How do I specify names for individual sheets in my Excel file?
When exporting to Excel, the sheets have names from the Dataset.title property (source code).
You can set the title in the Dataset constructor:
dataset = Dataset(title="Sheet name")