Pandas/Openpyxl - Save Current Date into xlsx Filename - pandas

Trying to save an xlsx file and include the current date in the file name during the process. Currently, I'm using the below code but I receive the error invalid format string - uncertain what format I can use to accomplish this.
I saw this method recommended in another thread but it doesn't work for me. I've tried several other solutions as well but nothing seems to work. Any guidance would be appreciated.
from openpyxl import load_workbook
from datetime import datetime, date
import os
from glob import glob
import pandas as pd
file = glob(
'C:\\Users\\all*.xlsx')[0]
wb1 = load_workbook(file)
ws1 = wb1.worksheets[0]
for row in ws1['A2':'D5']:
for cell in row:
cell.value = None
wb1.save('file1'+now.strftime("%Y%m%d%")+'.xlsx')

The error is in the save command where you have an extra % in the end. Also, just now is not sufficient, it needs the (). For the code above, think it also needs the datetime. to be added. So, change the last line from....
wb1.save('file1'+now.strftime("%Y%m%d%")+'.xlsx')
to
wb1.save('file1'+datetime.now().strftime("%Y%m%d")+'.xlsx')

Related

Change value of the column .csv file using Access VBA

I have a .dat file that I saved as a .csv and it imports in a table, OK. But this file has in the first column = HOUR "hnnss". The file name contains the date that I already managed to separate it and save it in a variable=date.
My problem is: when saving the file as .csv I need to open the file, change the values of the first column from hnnss to data hh:nn:ss and then save and close, only then do I import it into the table. It needs to be in that sequence. Thanks for help.
PS: I'm using Access 365 + VBA 7.1
Andreia: You can read the file using method like in here Import csv to array and in this loop :
For i = 2 To UBound(aryFile) - 1 ' in your case start with second line
tsOut.WriteLine aryFile(i)
Next
implement the formatting like this:
' in your case start with second line if the 1st is header
For i = 1 To UBound(aryFile) - 1
aryRow = Split(aryFile(i), ",")
aryRow(0) = formatTime(aryRow(0), aryRow(?)) ' replace ? with index of date field
tsOut.WriteLine Join(aryRow,",")
Next
The above code is using function formatTime(fieldWithTime, fieldWithDate) which you need to write and which returns your formatted string for the whole date. I leave it for you. If you won't be able to code it let me know but in that case you rather read some books about VBA programming.
Note: I did not debug the code. This is just an idea.

Proper usage of load_workbook in openpyxl when saving data to worksheet in existing file

I have a dataframe called results and an excel file named as vlpandas.xlsx. I set a default path for working dir as follows:
excel_dir = 'Users/Documents/Pythonfiles'
Based on the example from this post,: How to save a new sheet in an existing excel file, using Pandas?, I did the following:
book = load_workbook(excel_dir)
But I an error above after running the command, to fix it I use the usage as documented from the openpyxl and the following works:
book = load_workbook(filename = 'vlpandas.xlsx')
But then I get an exception error when I run the command below. Something about workbook.py from the openpyxl directory.
writer = pd.ExcelWriter(excel_dir, engine='openpyxl')
and then I want to complete my task saving the data to the new worksheet in the existing file vlpandas.xlsx with the following lines of code:
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
## Your dataframe to append.
results.to_excel(writer, '3rd_sheet')
writer.save()
So my two questions are:
1) What is the proper usage of load_Workbook from openpyxl
2) Why I am I getting an error with the command line below. I also changed my working folder path using the command osc.chdir:
writer = pd.ExcelWriter(excel_dir, engine='openpyxl')
Regards,
Gus

using openpyxl to update certain columns in a complex spreadsheet

Hi I have a pretty complex spreadsheet and I need to update "ONLY" certain part of its contents.
I tried openpyxl, which works ok except all the pivot tables, styles and formulas are lost, after save the workbook. Is there a way to get around that?
Check which version of openpyxl you have installed. Support for Pivot Tables was only added in version 2.5.0-a1 (2017-05-30), and that only supported one Pivot Table until version 2.5.0-b2 (2018-01-19). See changelog.
Alternatives: Simplify, xlwings, content protection, and win32com (most likely).
If you are using 2.5 and it doesn't work, you'll probably have to simplify your spreadsheet if you want to continue to use openpyxl. E.g. split it into a source and output file, and use python to update the source file.
You could try running xlwings to host the Python code inside of the spreadsheet itself.
Another possible solution would be to lock the contents - turn on protection - for all except the sections you plan to update. This is unlikely to work as python is changing the contents directly, but it may respect the lock.
Finally, the result most likely to work. Drive the changes through the win32com module. I think this, unlike openpyxl, requires Excel to be installed as it is using Excel's functionality to make the changes. Examples of usage are here and here.
A brief example (taken from here):
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Add()
ws = wb.Worksheets("Sheet1")
ws.Cells(1,1).Value = "Cell A1"
ws.Cells(1,1).Offset(2,4).Value = "Cell D2"
ws.Range("A2").Value = "Cell A2"
ws.Range("A3:B4").Value = "A3:B4"
ws.Range("A6:B7,A9:B10").Value = "A6:B7,A9:B10"
wb.SaveAs('ranges_and_offsets.xlsx')
excel.Application.Quit()

Python write data to Excel

I want to write some data from a CSV to an Excel-Sheet.
Here is my Code:
import xlrd
import xlsxwriter
import requests
# download
link="...."
s=requests.get(link).content
c=pd.read_csv(io.StringIO(s.decode('utf-8')))
book = xlrd.open_workbook("***.xlsx","w")
worksheet = book.sheet_by_index(10)
worksheet.write('B2',5)
workbook.close()
Actually i want to use the data from the requested link. But the code still stops on write a simple Issue. It works if i add a new sheet. But i cant write in an existing sheet. Have anyone an idea?

How to import part of a file name to a field

I have a file that i import into access 2007 and i was wondering if i can take part of that file name and put it into a field in access? For example here is one example of a file name:
"20140211_agent_statistics.csv"
I have done some research on this but cant seem to find the answer when numbers change all of the time. I just need to grab the numbers on this file name. However, these numbers change all of the time. Does anyone have a solution for this? Thank you in advance. Any help and code is much appreciated i am very new to vba.
Working on a few assumptions:
You are importing this by code so it picks up the file name?
The numbers are the date so probably always 8 characters long?
If you import by code you will assign the file name to a variable, in case you don't here is how to:
Dim strFileO as String, strFileLoc as String
strFileLoc = "C:\YourFolder\" ' Folder where file is saved
strFileO = Dir(strFileLoc & "*.csv")
Above will pick up any .csv file in the folder, you should move them once imported
Once you have the strFileo then to get the date:
Dim lDate as Long
lDate = Left(strFileO,8)
'Or if the numbers aren't always 8 characters:
lDate = Left(strFileO. InStr(strFileO,"_") - 1) ' Assumes numbers followed by "_"